1
一直在讀我最喜歡的程序員之一Douglas Crockford,特別是'方法'方法。用Javascript鏈接,爲什麼它在這段代碼中很有用?
的JavaScript:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
function myfunc(value) {
this.value=value;
}
myfunc.method('toString', function() {
return this.value;
});
var testvar = new myfunc('myself').toString();
alert(testvar);
我感到困惑關於return this
。
return this
這是什麼意思?
該方法沒有它,在我讀過的所謂的鏈接中,但我怎樣才能使用這個'方法'函數使用鏈接,爲什麼它有用?
謝謝
哦不對所以這就是爲什麼它的有用的,這就是如何落實到我的代碼。非常感激 –