当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 关于__defineGetter__ 和__defineSetter__的说明

Javascript
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
JavaScript与C# Windows应用程序交互方法
javascript之解决IE下不渲染的bug
javascript之锁定表格栏位
javascript实现鼠标选取拖动或Ctrl选取拖动
优化网页之快速的呈现我们的网页
javascritp实现input输入框相关限制用法
用javascript实现的激活输入框后隐藏初始内容
CSS代码格式化和压缩的方法与技巧
autocomplete禁止自动完成功能
IE autocomplete internet explorer''s autocomplete
如何快速的呈现我们的网页的技巧整理
计算黄金分割的javascript代码
设置和读取cookie的javascript代码
javascript 复选框选择/全选后特效
实现一个年、月、季度联动SELECT的javascript代码
javascript事件模型代码
WordPress 插件——CoolCode使用方法与下载
用javascript实现画图效果的代码
javascript发表评论或者留言时的展开效果

Javascript 中的 关于__defineGetter__ 和__defineSetter__的说明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 59 ::
收藏到网摘: n/a

作者:anbutu
来源:http://anbutu.javaeye.com/blog/post/194276
关键字: JavaScript Mozilla __defineGetter__ __defineSetter__
Getter是一种获取一个属性的值的方法,Setter是一种设置一个属性的值的方法。可以为任何预定义的核心对象或用户自定义对象定义getter和setter方法,从而为现有的对象添加新的属性。
有两种方法来定义Getter或Setter方法:
在对象初始化时定义
在对象定义后通过Object的__defineGetter__、__defineSetter__方法来追加定义
在使用对象初始化过程来定义Getter和Setter方法时唯一要做的事情就是在getter方法前面加上“get”,在setter方法前面加上“set”。
还有一点要注意的就是getter方法没有参数,setter方法必须有一个参数,也就是要设置的属性的新值。
例如:
复制代码 代码如下:

o = {
value:9,
get b() {return this.value;},
set setter(x) {this.value = x;}
}

在对象定义后给对象添加getter或setter方法要通过两个特殊的方法__defineGetter__和__defineSetter__。这两个函数要求第一个是getter或setter的名称,以string给出,第二个参数是作为getter或setter的函数。
例如我们给Date对象添加一个year属性:
复制代码 代码如下:

Date.prototype.__defineGetter__('year', function() {return this.getFullYear();});
Date.prototype.__defineSetter__('year', function(y) {this.setFullYear(y)});
var now = new Date;
alert(now.year);
now.year = 2006;
alert(now);

至于采用哪种形式主要取决于个人的编程风格,采用第一种形式结构紧凑,更容易理解。但是假如你想在对象定义以后再添加Getter或Setter,或者这个对象的原型不是你写的或是内置对象,那么只好采用第二种方式了。
下面是一个为Mozilla浏览器添加innerText属性的实现:
复制代码 代码如下:

HTMLElement.prototype.__defineGetter__
(
"innerText",function()
//define a getter method to get the value of innerText,
//so you can read it now!
{
var textRange = this.ownerDocument.createRange();
//Using range to retrieve the content of the object
textRange.selectNodeContents(this);
//only get the content of the object node
return textRange.toString();
// give innerText the value of the node content
}
);