Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've created a simple Javascript library to be able to create "classes" and extend them defining instance and static methods and variables.

It consists in a simple function Class with two methods: create and extend.

var Class = function(){};

Class.extend = function(obj){

    var Extended = function(){};

    for(var key in this.prototype){
        Extended.prototype[key] = this.prototype[key];
    }

    for(var key in obj){
        Extended.prototype[key] = obj[key];
    };

    Extended.prototype.constructor = Extended; 

  for (var key in this) {
    Extended[key] = this[key];
  };

  return Extended;
};

Class.create = function(constructor){
    var created = new this();
    for(var key in constructor){
        created[key] = constructor[key];
    }
    return created;
};

Here there's an example of what the library is able to do:

var Model = Class.extend({
    fields: {},
    save: function(){
        return "saving a model at " + Model.url;
    },
    destroy: function(){
        return "destroying...";
    }
});

Model.url = "http://localhost";

Model.all = function(){
    return "all from " + Model.url;
};

Model.find = function(id){
    return "finding by " + id;
};

var User = Model.extend({
    fields: {
        username: "default username",
        password: "default password",
    },
    logout: function(){
        return "logged out " + this.fields.username;
    },
    destroy: function(){
        return this.logout() + " and account destroyed!";
    }
});

User.url = Model.url + "/users";

User.all = function(){
    return "overriding parent! all from " + User.url;
}

User.findByUsername = function(username){
    return "finding by username: " + username;
};

And I've created a js fiddle to test the example http://jsfiddle.net/R6jGe/

There's no way to call a parent instance method or variable.

how do you think the library could be improved?

share|improve this question
Fixed your code – Raynos Jun 24 '12 at 14:12
@Raynos: you have lost distinction between static methods vs instance methods. Matteo: you are unable to have private variables share between privileged class methods. – Bill Barry Jun 25 '12 at 20:41
@BillBarry there is no distinction between static & instance. private variables have no value. privileged methods are silly – Raynos Jun 25 '12 at 21:01
@Raynos i need distinction between static and instance methods. – Matteo Pagliazzi Jun 26 '12 at 9:53
@BillBarry mmm... for private variables I may use the module pattern but i don't think i need them for now... but how to share them between privileged methods? – Matteo Pagliazzi Jun 26 '12 at 9:53
show 4 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.