[英]How to correctly share a variable between multiple prototype functions?
因此,基本上我想做的事在我的示例中應該是清楚的,即跨原型內的多個函數訪問原型內的變量。 我不確定如何正確處理此問題,因此請提供以下代碼示例,說明如何正確處理共享變量。
我正在為我的項目使用Phaser游戲狀態,這只是我認為的用法的一般示例。 我是原型的新手,所以仍然要學習它。
var x = x || {};
var sharedVariable;
x.prototype = {
function1: function() {
console.log(sharedVariable);
},
function2: function() {
console.log(sharedVariable);
}
}
請檢查一下。
function x(){ this.sharedVariable = 1; } x.prototype = { function1: function(){ console.log(this.sharedVariable++); }, function2: function(){ console.log(this.sharedVariable++); } } var y = new x(); y.function1(); //print 1 y.function2(); //print2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.