Custom Search

Thursday, June 28, 2012

Howto Javascript Prototype Pattern

 1)

a = {x:100};
alert(a.x);

f1 = function MyFun(){}
f1.prototype = a
    
b = new f1();
alert(b.x);    

====== OR =====

 2)

var clone = (function () {
    function F() {} //Note : anonymous function
    return function ( proto_obj ) {
        F.prototype = proto_obj;
        return new F();
    };
})(); // Note : '()' is required

c = clone(b);
alert(c.x);  


OUTPUT ALERT
=============
alert(a.x);   100
alert(b.x);   100
alert(c.x);   100