2007年12月17日月曜日

applyが少し理解できたかもしれない

function Foo() {
 
this.fooValue = 1;
 
this.fooFunc = function () { alert(this.fooValue); }
}
 
function Bar() {
 
Foo.apply(this);
//   this.superFooFunc = this.fooFunc; // これでもいいけど、外からアクセスできてしまう。(A)
 
var superFooFunc = this.fooFunc; // これだと、この関数内からのみアクセス可能。(B)
 
 
this.fooFunc = function () {
    alert
("fooFunc");
// this.superFooFunc(); // (A)のケースだとこちらの呼び出しでOK
    superFooFunc
.apply(this); // (B)のケースだと、こっちになる
 
}
}
 
var a = new Bar();
a
.fooFunc();

0 件のコメント: