//詳細例子在widget範圍
$.widget("ui.staticTest", {
//staticVar is an instance variable, regardless of what it is named as
staticVar: 'brownMamba',
_create: function() {
},
//test the static variable
testStatic: function(a) {
if (a) {
//Here you're actually creating a new static variable called
//staticVar which is associated with the staticTest object as you assign
//value to it.
//Lemme show you what I mean with an example
//Here it alerts 'Undefined' as 'staticVar' does not exists
//don't confuse it with the staticVar: 'brownMamba' declared above,
//that one is an instance variable
alert("Type of $.ui.staticTest.staticVar before assignment: " + typeof $.ui.staticTest.staticVar);
$.ui.staticTest.staticVar = a;
//At this point it alerts the type of 'a', which in our case is 'string'
alert("Type of $.ui.staticTest.staticVar after assignment: " + typeof $.ui.staticTest.staticVar);
//value of instance variable at this point
alert("typeof this.staticVar: " + typeof this.staticVar);
alert("Value of this.staticVar: " + this.staticVar);
//value of global variable at this point
//'Undefined' as it does not exist
alert("Type of staticVar: " + typeof staticVar); //or window.staticVar
} else {
alert("value of staticVar in testStatic with no argument: " + $.ui.staticTest.staticVar);
}
},
//test the instance variable
testInstance: function(a) {
if (a) {
//Here you're actually working with the instance variable declared above,
//with the value 'brownMamba'
//Lemme show you what I mean with an example
//Here it alerts 'string' as 'staticVar' exists and is assigned a string
alert("typeof this.staticVar is " + typeof this.staticVar + " and its
value is " + this.staticVar);
this.staticVar = a;
alert("typeof this.staticVar after assignment is " + typeof this.staticVar);
alert("Value of this.staticVar after assignment is " + this.staticVar);
} else {
alert("value of this.staticVar in testInstance with no argument: " + this.staticVar);
}
},
//test the Global variable
testGlobal: function(a) {
if (a) {
/*Here you're actually creating a global variable called staticVar*/
//Lemme show you what I mean with an example
//Here it alerts 'Undefined' as 'staticVar' does not exists
alert("Type of staticVar before assignment: " + typeof staticVar);
staticVar = a; //creating a global variable, which will be declared
in window scope, i.e. window.staticVar
//At this point it alerts the type of a, which in our case is a 'string'
alert("Type staticVar after assignment: " + typeof staticVar);
alert("Value of staticVar after assignment: " + staticVar)
} else {
alert("value of staticVar in testGlobal with no argument: " + staticVar);
}
}
});
//instantiating widget
$('#test').staticTest();
//instantiating widget
$('#parent').staticTest();
$('#test').staticTest('testStatic', 'changed');
//value will be sustained as its associated to the object
$('#parent').staticTest('testStatic');
$('#test').staticTest('testInstance', 'bluemamba');
//here the value doesn't change as its an instance variable and its value is not altered
$('#parent').staticTest('testInstance');
$('#test').staticTest('testGlobal', 'bluemamba');
//here the value is still sustained as the global is created in the previous call
$('#parent').staticTest('testGlobal');
http://jsfiddle.net/hiteshubharani/z5k4E/6/
這是一個很好的答案,非常簡單,與其他答案不同,它允許靜態變量真正是私有的。 – alzclarke 2012-05-24 17:36:30
我可以問一個人如何從widget外部訪問(設置)staticVariable嗎?我需要一個可以爲所有窗口小部件實例配置一次的選項。 – Marc 2012-11-03 16:10:58
您可以創建一個函數來使其具有全局範圍 – 2012-11-09 16:48:53