我儘量不重複明顯的問題,因爲我已經看到了關於道格拉斯Crockford的JavaScript中的好零件手冊Function.prototype.method什麼是原型[名]
我瞭解大部分代碼
的一些問題/答案Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Function.method('inherits', function (Parent) {
this.prototype = new Parent();
return this;
});
var Mammal = function (name) {
this.name = name;
}.method('get_name', function() {
return this.name;
}).method('says', function() {
return this.saying || '';
});
var Cat = function (name) {
this.name = name;
this.saying = 'meow';
}.inherits(Mammal)
var myCat = new Cat('bagsley');
myCat.get_name();
我得到的麻煩是this.prototype [name]爲什麼不寫成this.prototype.name;我知道這回可鏈接和這裏的語法看起來非常相似,jQuery的,但我仍然沒有得到原型[名]部分
任何幫助apprecaited 感謝
http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets – Jack
它分配的參數,以一個新的對象,這就是爲什麼能」不要使用點符號。 – elclanrs
@elclanrs不,不能使用點符號,因爲屬性名稱是動態字符串。 –