それはかなり簡単です:
function Ninja(name)
{//this points to Ninja object inside constructor scope
this.name = name;
this.changeName = function(newname)
{//method of Ninja, this points to context object, ie Ninja
this.name = newname;//accesses the name property of Ninja
this.anotherFunction = function(newname2)
{//defines new method for this ==> Ninja object
this.name2 = newname2;//context object is Ninja
};
};
}
var foo = new Ninja('foobar');
foo.name;//foobar
foo.changeName;//function, is defined in constructor
typeof foo.anotherFunction//undefined because it's assigned when the changeName method is called!
foo.changeName('Me');
foo.name;//Me
foo.anotherFunction('You');//works: changeName was called, and assigned anotherFunction to foo (instance of Ninja)
foo.name2;//You
何が起こったのか:changeNameメソッドを呼び出すことにより、単純anotherFunction
に定義され、に割り当てられましたthis
。その時点でオブジェクトをthis
参照していたため、メソッドは、メソッドが呼び出されNinja
たインスタンスに割り当てられました。メソッドを呼び出す前は、メソッドは単に存在していませんでした。Ninja
changeName
changeName
anotherFunction
これは役に立たないか愚かに見えるかもしれませんが、それは理にかなっています。覚えておく必要があるのは、関数/メソッドは本質的にスタンドアロンオブジェクトであるということです。このコードでは、それらはたまたまプロパティ/メソッドとして定義されていますが、そのように使用する必要はありません。上記のコードに戻ります。
foo.name;//Me
bar = {};//some object
foo.changeName.apply(bar,['You']);
foo.name;//Me, nothing has changed, the changeName method was applied to the bar object
bar.name;//You, in the apply call, this pointed to bar, not foo
typeof bar.anotherFunction;//function
//You could even create the anotherFunction globally:
// JUST TO SHOW YOU CAN, THIS IS DANGEROUS
// ONLY USE THIS IF YOU KNOW WHAT THE POSSIBLE CONSEQUESES ARE!
foo.changeName.call();//in global scope
typeof antoherFunction;//function ==> this function is now globally available
このメソッドは、任意のchangeName
オブジェクトに適用でき、新しいメソッドを追加したり、特定のインスタンスに特定のプロパティを変更/追加したりできます。