3
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Fixed your code \$\endgroup\$ – Raynos Jun 24 '12 at 14:12
  • \$\begingroup\$ @Raynos: you have lost distinction between static methods vs instance methods. Matteo: you are unable to have private variables share between privileged class methods. \$\endgroup\$ – Bill Barry Jun 25 '12 at 20:41
  • \$\begingroup\$ @BillBarry there is no distinction between static & instance. private variables have no value. privileged methods are silly \$\endgroup\$ – Raynos Jun 25 '12 at 21:01
  • \$\begingroup\$ @Raynos i need distinction between static and instance methods. \$\endgroup\$ – Matteo Pagliazzi Jun 26 '12 at 9:53
  • \$\begingroup\$ @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? \$\endgroup\$ – Matteo Pagliazzi Jun 26 '12 at 9:53
3
\$\begingroup\$

I read your code several times, and I could only find few observations:

  • jQuery has extend as well, the idea of it is sound, you could consider working with arguments and allow for n objects like jQuery does
  • I am not sure that always overriding the constructor is a good idea in extend
  • In the same vein, your approach does not allow for custom constructors that take parameters, whereas most OO classes have those ( new Customer( "Bob" ) to give a silly example)
  • From a lint perspective:
    • You declare var key thrice in extend, you only need to declare it once, preferably on top
    • for loop blocks do not need semicolons
    • You should really look into .hasOwnProperty(), I am pretty sure your code can cause subtle bugs by smushing everything together
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.