高手提领 之 Douglas Crockford: "Advanced JavaScript"

视频地址:http://video.yahoo.com/watch/111585/1027823
PPT下载:http://yuiblog.com/assets/crockford/advancedjavascript.zip

 

关于闭包(Closure)

Flexible, but Why we do that?

 

////////////////////////////////////////

Regular Singleton :

var singleton = {

    firstMethod: function (a, b) {

        ...

    },

    secondMethod: function (c) {

        ...

    }       

};

////////////////////////////////////////

For Private Variables:

var singleton = function () {

    var privateVariable;

    function privateFunction(x) {

        ...privateVariable...

    }

 

    return {

        firstMethod: function (a, b) {

            ...privateVariable...

        },

        secondMethod: function (c) {

            ...privateFunction()...

        }

     };

}();

////////////////////////////////////////

Return Private Object:

function powerConstructor() {

    var that = object(oldObject),

        privateVariable;

    function privateFunction(x) {}

 

    that.firstMethod = function (a, b) {

        ...privateVariable...

    };

    that.secondMethod = function (c) {

        ...privateFunction()...

    };

    return that;

}

////////////////////////////////////////

Regular Inheritance:

function Gizmo(id) {

    this.id = id;

}

Gizmo.prototype.toString = function () {

    return "gizmo " + this.id;

};

 

function Hoozit(id) {

    this.id = id;

}

Hoozit.prototype = new Gizmo();

Hoozit.prototype.test = function (id) {

    return this.id === id;

};

////////////////////////////////////////

Inheritance save code:

function gizmo(id) {

    return {

        id: id,

        toString: function () {

            return "gizmo " + this.id;

        }

    };

}

 

function hoozit(id) {

    var that = gizmo(id);

    that.test = function (testid) {

        return testid === this.id;

    };

    return that;

}

////////////////////////////////////////

Private Property:

function gizmo(id) {

    return {

        toString: function () {

            return "gizmo " + id;

        }

    };

}

 

function hoozit(id) {

    var that = gizmo(id);

    that.test = function (testid) {

        return testid === id;

    };

    return that;

}

////////////////////////////////////////

Protected Property:

function gizmo(id, secret) {

    secret = secret || {};

    secret.id = id;

    return {

        toString: function () {

            return "gizmo " + secret.id;

        };

    };

}

 

function hoozit(id) {

    var secret = {},    /*final*/

        that = gizmo(id, secret);

    that.test = function (testid) {

        return testid === secret.id;

    };

    return that;

}

////////////////////////////////////////

Call Super Function:

function hoozit(id) {

    var secret = {},

         that = gizmo(id, secret),

        super_toString = that.toString;

    that.test = function (testid) {

        return testid === secret.id;

    };

    that.toString = function () {

        return super_toString.apply(that, []);

    };

    return that;

}

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(javascript,function,inheritance,variables,object,c,Reference)