JavaScript Accessor實現(xiàn)說明
更新時間:2010年12月06日 21:34:41 作者:
關于Getter與Setter大家一定不會陌生,下面簡單介紹幾種我所知道的在JavaScript中實現(xiàn)G/S的方法.
第一種算是比較常見了,通過閉包Store Value從而實現(xiàn)accessor,適用于所有瀏覽器.
function Sandy(val){
var value = val;
this.getValue = function(){
return value;
};
this.setValue = function(val){
value = val;
};
}
//usage
var sandy = new Sandy("test");
sandy.value
// => undefined
sandy.setValue("test2")
sandy.getValue
下面是JavaScript權威指南(中文第五版)中P152頁使用閉包的一個例子.
function makeProperty(o, name, predicate) {
var value; //This is property value;
//The setter method simply returns the value
o['get' + name] = function() { return value;};
//The getter method stores the value or throws an exception if
//the predicate rejects the value
o['set' + name] = function(v) {
if (predicate && !predicate(v) {
throw 'set' + name + ': invalid value ' + v;
} else {
value = y;
}
}
}
//The following code demenstrates the makeProperty() method
var o = {}; // Here is an empty object
//Add property accessor methods getName and setName
//Ensure that only string values are allowed
makeProperty(o, 'Name', function(x) { return typeof x == 'string'; });
o.setName('Frank'); //Set the property value;
print(o.getName()); //Get the property value
o.setName(0); //Try to set a value of the wrong type
第二種方法是使用__defineSetter__與__defineGetter__來實現(xiàn)accessor,看下劃線就知道它們并非標準,適用于Firefox 2.0+, Safari 3.0+, Google Chrome 1.0+ 和 Opera 9.5+ ,方法使用見MDN.
function Sandy(val){
var value = val,
_watch = function(newVal) {
console.log('val is Changed to : ' + newVal);
}
this.__defineGetter__("value", function(){
return value;
});
this.__defineSetter__("value", function(val){
value = val;
_watch(val);
});
}
var sandy = new Sandy("test");
sandy.value
// => test
sandy.value = "test2";
// => 'val is Changed to : test2'
sandy.value
// => "test2"
除了__defineG/Setter__外, 你還可以使用'set'、'get'關鍵字在在原型對象上定義accessor,對于單個對象同樣適用, 適用于Firefox 2.0+, Safari 3.0+, Google Chrome 1.0+ 和 Opera 9.5+.
function Sandy(val){
this.value = val;
}
Sandy.prototype = {
get value(){
return this._value;
},
set value(val){
this._value = val;
}
};
//Or
var sandy = {
'_value' : 'sandy',
get value() {
return this._value;
},
set value(val) {
this._value = val;
}
}
最后一種方法,用到了Object的靜態(tài)方法defineProperty,作用于單個對象,該方法應該屬于ES5的范疇了,目前似乎只有Chrome 支持這種方法,其實Ie8也支持,但操作對象僅限于Dom節(jié)點(Dom node),見IEBlog,該方法的使用見MDN.
var sandy = {}, rValue;
Object.defineProperty(sandy, 'value' ,
{
'set' : function(val) {
rValue = val;
},
'get' : function() {
return rValue;
},
'enumerable' : true,
'configurable' : true
}
)
//Ie8+
Object.defineProperty(document.body, "description", {
get : function () {
return this.desc;
},
set : function (val) {
this.desc = val;
}
});
document.body.description = "Content container";
// document.body.description will now return "Content container"
‘enumerable','configuralbe' 屬于ES5規(guī)范中的Property Attributes(屬性特性),在這里就不做討論了,有興趣的Google或者直接去看ES5的文檔. ^ ^
復制代碼 代碼如下:
function Sandy(val){
var value = val;
this.getValue = function(){
return value;
};
this.setValue = function(val){
value = val;
};
}
//usage
var sandy = new Sandy("test");
sandy.value
// => undefined
sandy.setValue("test2")
sandy.getValue
下面是JavaScript權威指南(中文第五版)中P152頁使用閉包的一個例子.
復制代碼 代碼如下:
function makeProperty(o, name, predicate) {
var value; //This is property value;
//The setter method simply returns the value
o['get' + name] = function() { return value;};
//The getter method stores the value or throws an exception if
//the predicate rejects the value
o['set' + name] = function(v) {
if (predicate && !predicate(v) {
throw 'set' + name + ': invalid value ' + v;
} else {
value = y;
}
}
}
//The following code demenstrates the makeProperty() method
var o = {}; // Here is an empty object
//Add property accessor methods getName and setName
//Ensure that only string values are allowed
makeProperty(o, 'Name', function(x) { return typeof x == 'string'; });
o.setName('Frank'); //Set the property value;
print(o.getName()); //Get the property value
o.setName(0); //Try to set a value of the wrong type
第二種方法是使用__defineSetter__與__defineGetter__來實現(xiàn)accessor,看下劃線就知道它們并非標準,適用于Firefox 2.0+, Safari 3.0+, Google Chrome 1.0+ 和 Opera 9.5+ ,方法使用見MDN.
復制代碼 代碼如下:
function Sandy(val){
var value = val,
_watch = function(newVal) {
console.log('val is Changed to : ' + newVal);
}
this.__defineGetter__("value", function(){
return value;
});
this.__defineSetter__("value", function(val){
value = val;
_watch(val);
});
}
var sandy = new Sandy("test");
sandy.value
// => test
sandy.value = "test2";
// => 'val is Changed to : test2'
sandy.value
// => "test2"
除了__defineG/Setter__外, 你還可以使用'set'、'get'關鍵字在在原型對象上定義accessor,對于單個對象同樣適用, 適用于Firefox 2.0+, Safari 3.0+, Google Chrome 1.0+ 和 Opera 9.5+.
復制代碼 代碼如下:
function Sandy(val){
this.value = val;
}
Sandy.prototype = {
get value(){
return this._value;
},
set value(val){
this._value = val;
}
};
//Or
var sandy = {
'_value' : 'sandy',
get value() {
return this._value;
},
set value(val) {
this._value = val;
}
}
最后一種方法,用到了Object的靜態(tài)方法defineProperty,作用于單個對象,該方法應該屬于ES5的范疇了,目前似乎只有Chrome 支持這種方法,其實Ie8也支持,但操作對象僅限于Dom節(jié)點(Dom node),見IEBlog,該方法的使用見MDN.
復制代碼 代碼如下:
var sandy = {}, rValue;
Object.defineProperty(sandy, 'value' ,
{
'set' : function(val) {
rValue = val;
},
'get' : function() {
return rValue;
},
'enumerable' : true,
'configurable' : true
}
)
//Ie8+
Object.defineProperty(document.body, "description", {
get : function () {
return this.desc;
},
set : function (val) {
this.desc = val;
}
});
document.body.description = "Content container";
// document.body.description will now return "Content container"
‘enumerable','configuralbe' 屬于ES5規(guī)范中的Property Attributes(屬性特性),在這里就不做討論了,有興趣的Google或者直接去看ES5的文檔. ^ ^
相關文章
javascript實現(xiàn)動態(tài)加載CSS
最近在做自己的小框架的按需加載模塊,那么就需要做到異步動態(tài)加載css文件。仔細研究了一番,得到了如下解決方案,分享給大家。2015-01-01js實現(xiàn)翻頁后保持checkbox選中狀態(tài)的實現(xiàn)方法
在項目中有需求如下:上下分頁后,選中的checkbox狀態(tài)保持不變2012-11-11BootStrap 智能表單實戰(zhàn)系列(二)BootStrap支持的類型簡介
這篇文章主要介紹了BootStrap 智能表單實戰(zhàn)系列(二)BootStrap支持的類型簡介 的相關資料,非常不錯具有參考借鑒價值,感興趣的朋友一起學習吧2016-06-06微信小程序?qū)崿F(xiàn)一個簡單swiper代碼實例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)一個簡單swiper代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12