2008年2月11日月曜日

prototypeはchainする

A = function() {}
A
.prototype.myfunc = function() {
    alert
("A's myfunc")
}

B
= function() {}
B
.prototype = new A();
B
.prototype.myfunc = function() {
    alert
("B's myfunc")
}

b1
= new B();
b1
.myfunc = function() {
    alert
("b1's myfunc")
}
b1
.myfunc(); // b1's myfunc

b2
= new B();
b2
.myfunc(); // b2's myfunc not found -> B.prototype's myfunc

delete B.prototype.myfunc

b3
= new B();
b3
.myfunc(); // b3's myfunc not found -> B.prototype's myfunc not found -> A.prototype's myfunc

0 件のコメント: