我試圖做一個沒有原型的類。這裏是一個例子:`this`裏面的函數不是一個類的原型
test = (function() {
this.value = 1;
this.print = function() {
console.log(this.value);
};
return this;
})();
這個完美的按預期工作。我不明白的是this.value
裏面的this.print
函數。 this.print
如何正確知道任何提及this
是指test
而不是window
?通過this.___ = function(){}
定義的任何函數是否會自動將this
作爲上下文添加?
[JavaScript中的私人成員](http://javascript.crockford.com/private.html) - 閱讀本文。 – dirkgently
對於像我這樣使用coffeescript的人,你可以通過在你的函數中使用'=>'而不是' - >'來像在http://stackoverflow.com/a/10965498/927092中那樣關閉'this'變量'聲明 –