JavaScript Accessor實(shí)現(xiàn)說明
更新時(shí)間:2010年12月06日 21:34:41 作者:
關(guān)于Getter與Setter大家一定不會(huì)陌生,下面簡(jiǎn)單介紹幾種我所知道的在JavaScript中實(shí)現(xiàn)G/S的方法.
第一種算是比較常見了,通過閉包Store Value從而實(shí)現(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權(quán)威指南(中文第五版)中P152頁使用閉包的一個(gè)例子.
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__來實(shí)現(xiàn)accessor,看下劃線就知道它們并非標(biāo)準(zhǔn),適用于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'關(guān)鍵字在在原型對(duì)象上定義accessor,對(duì)于單個(gè)對(duì)象同樣適用, 適用于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,作用于單個(gè)對(duì)象,該方法應(yīng)該屬于ES5的范疇了,目前似乎只有Chrome 支持這種方法,其實(shí)Ie8也支持,但操作對(duì)象僅限于Dom節(jié)點(diǎn)(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的文檔. ^ ^
復(fù)制代碼 代碼如下:
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權(quán)威指南(中文第五版)中P152頁使用閉包的一個(gè)例子.
復(fù)制代碼 代碼如下:
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__來實(shí)現(xiàn)accessor,看下劃線就知道它們并非標(biāo)準(zhǔn),適用于Firefox 2.0+, Safari 3.0+, Google Chrome 1.0+ 和 Opera 9.5+ ,方法使用見MDN.
復(fù)制代碼 代碼如下:
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'關(guān)鍵字在在原型對(duì)象上定義accessor,對(duì)于單個(gè)對(duì)象同樣適用, 適用于Firefox 2.0+, Safari 3.0+, Google Chrome 1.0+ 和 Opera 9.5+.
復(fù)制代碼 代碼如下:
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,作用于單個(gè)對(duì)象,該方法應(yīng)該屬于ES5的范疇了,目前似乎只有Chrome 支持這種方法,其實(shí)Ie8也支持,但操作對(duì)象僅限于Dom節(jié)點(diǎn)(Dom node),見IEBlog,該方法的使用見MDN.
復(fù)制代碼 代碼如下:
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的文檔. ^ ^
相關(guān)文章
javascript實(shí)現(xiàn)動(dòng)態(tài)加載CSS
最近在做自己的小框架的按需加載模塊,那么就需要做到異步動(dòng)態(tài)加載css文件。仔細(xì)研究了一番,得到了如下解決方案,分享給大家。2015-01-01js實(shí)現(xiàn)翻頁后保持checkbox選中狀態(tài)的實(shí)現(xiàn)方法
在項(xiàng)目中有需求如下:上下分頁后,選中的checkbox狀態(tài)保持不變2012-11-11BootStrap 智能表單實(shí)戰(zhàn)系列(二)BootStrap支持的類型簡(jiǎn)介
這篇文章主要介紹了BootStrap 智能表單實(shí)戰(zhàn)系列(二)BootStrap支持的類型簡(jiǎn)介 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06使用uniapp實(shí)現(xiàn)發(fā)布朋友圈功能
這篇文章主要介紹了使用uniapp實(shí)現(xiàn)發(fā)布朋友圈功能,在文章底部給大家介紹了uniapp?微信小程序分享、分享朋友圈功能,通過頁內(nèi)自定義分享按鈕,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09JS焦點(diǎn)圖,JS 多個(gè)頁面放多個(gè)焦點(diǎn)圖的實(shí)例
下面小編就為大家?guī)硪黄狫S焦點(diǎn)圖,JS 多個(gè)頁面放多個(gè)焦點(diǎn)圖的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12xmlplus組件設(shè)計(jì)系列之樹(Tree)(9)
xmlplus 是一個(gè)JavaScript框架,用于快速開發(fā)前后端項(xiàng)目。這篇文章主要介紹了xmlplus組件設(shè)計(jì)系列之tree,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05微信小程序?qū)崿F(xiàn)一個(gè)簡(jiǎn)單swiper代碼實(shí)例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)一個(gè)簡(jiǎn)單swiper代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12