【謎】prototype。
ようやくprototypeの必要性と役割を学ぶところまで来た。でもまだまだ謎がいっぱい。
// **** prototypeなし、オブジェクトで呼び出し **** String.yeah = function(){ alert('yeah!!'); } String.yeah(); // yeah!! // **** prototypeなし、インスタンスで呼び出し **** String.yeah = function(){ alert('yeah!!'); } 'y'.yeah(); // エラー // **** prototypeあり、インスタンスで呼び出し **** String.prototype.yeah = function(){ alert('yeah!!'); } 'y'.yeah(); // yeah!!
インスタンスのメソッド・プロパティを新規に作る際はprototypeが必須、
オブジェクトからの呼び出しオンリーならprototype不要ということ?