my favorite

2010/05/29

extend in YUI

    extend: function(subc, superc, overrides) {
        if (!superc||!subc) {
            throw new Error("extend failed, please check that " +
                            "all dependencies are included.");
        }
        var F = function() {}, i;
        F.prototype=superc.prototype;
        subc.prototype=new F();
        subc.prototype.constructor=subc;
        subc.superclass=superc.prototype;
        if (superc.prototype.constructor == OP.constructor) {
            superc.prototype.constructor=superc;
        }
    
        if (overrides) {
            for (i in overrides) {
                if (L.hasOwnProperty(overrides, i)) {
                    subc.prototype[i]=overrides[i];
                }
            }

            L._IEEnumFix(subc.prototype, overrides);
        }
    },

3 comments:

huang47 said...

Actually my understanding is that xxx.prototype.constructor equals to xxx.
I have to figure out why we need to do this assignment in line 09

huang47 said...

subc.prototype = new F();
in this line of code we use F constructor to implement (or fill) subc.prototype
and it's constructor would be different with original subc now.

if we don't modify subc.prototype.constructor, later we call new subc() will be equal to call new function(){}. it's not exactly what we want here.

huang47 said...

Refer : http://phrogz.net/js/classes/OOPinJS2.html

// Here's where the inheritance occurs
Cat.prototype = new Mammal();

// Otherwise instances of Cat would have a constructor of Mammal
Cat.prototype.constructor=Cat;

BIO

Taipei, GuTing, Taiwan

huang47 | personal

huang47 | personal