JavaScript中Get和Set访问器的实现
今天要和大家分享的是JavaScript中的Get和Set访问器,和C#中的访问器非常相似。
标准的Get和Set访问器的实现
function Field(val){ this.value = val; } Field.prototype = { get value(){ return this._value; }, set value(val){ this._value = val; }};var field = new Field("test");field.value="test2";//field.value will now return "test2" 在如下浏览器能正常工作:
我们常用的实现方法可能是这样的:
function Field(val){ var value = val; this.getValue = function(){ return value; }; this.setValue = function(val){ value = val; };}var field = new Field("test");field.setValue("test2")field.getValue() // return "test2"
在DOM元素上Get和Set访问器的实现
HTMLElement.prototype.__defineGetter__("description", function () { return this.desc; }); HTMLElement.prototype.__defineSetter__("description", function (val) { this.desc = val; }); document.body.description = "Beautiful body"; // document.body.description will now return "Beautiful body";
在如下浏览器能正常工作:
将来ECMAScript标准的扩展对象的方法会通过Object.defineProperty来实现,这也是为什么IE8就是通过这种方法来实现get和set访问器,看来微软还是很有远见的,遗憾的是目前只有IE8+和Chrome 5.0+支持,其它的浏览器都不支持,而且IE8+也只支持DOM元素,不过将来的版本将和Chrome一样支持普通的Javascript对象。
DOM元素上的Get和Set访问器的实现
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"
在如下浏览器能正常工作:
普通对象的Get和Set访问器的实现
var lost = { loc : "Island"}; Object.defineProperty(lost, "location", { get : function () { return this.loc; }, set : function (val) { this.loc = val; }});lost.location = "Another island";// lost.location will now return "Another island" 在如下浏览器能正常工作:
标准的Get和Set访问器的实现
function Field(val){ this.value = val; } Field.prototype = { get value(){ return this._value; }, set value(val){ this._value = val; }};var field = new Field("test");field.value="test2";//field.value will now return "test2" 在如下浏览器能正常工作:
我们常用的实现方法可能是这样的:
function Field(val){ var value = val; this.getValue = function(){ return value; }; this.setValue = function(val){ value = val; };}var field = new Field("test");field.setValue("test2")field.getValue() // return "test2"
在DOM元素上Get和Set访问器的实现
HTMLElement.prototype.__defineGetter__("description", function () { return this.desc; }); HTMLElement.prototype.__defineSetter__("description", function (val) { this.desc = val; }); document.body.description = "Beautiful body"; // document.body.description will now return "Beautiful body";
在如下浏览器能正常工作:
将来ECMAScript标准的扩展对象的方法会通过Object.defineProperty来实现,这也是为什么IE8就是通过这种方法来实现get和set访问器,看来微软还是很有远见的,遗憾的是目前只有IE8+和Chrome 5.0+支持,其它的浏览器都不支持,而且IE8+也只支持DOM元素,不过将来的版本将和Chrome一样支持普通的Javascript对象。
DOM元素上的Get和Set访问器的实现
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"
在如下浏览器能正常工作:
普通对象的Get和Set访问器的实现
var lost = { loc : "Island"}; Object.defineProperty(lost, "location", { get : function () { return this.loc; }, set : function (val) { this.loc = val; }});lost.location = "Another island";// lost.location will now return "Another island" 在如下浏览器能正常工作:
【JavaScript中Get和Set访问器的实现】相关文章
4. 一个跨浏览器的响应式设计辅助工具类库 - CSS Browser Selector +
5. math.js :用于JavaScript和Node.js的数据扩展库
7. DalekJS – 基于 JavaScript 实现跨浏览器的自动化测试
9. 采用HTML5技术实现的远程桌面访问,Guacamole 0.6.4 发布
10. TogetherJS:一个免费开源的JavaScript库
本文来源:https://www.51html5.com/a3390.html
﹝JavaScript中Get和Set访问器的实现﹞相关内容
- 开发者应该关注的五项Web新兴技术:WebGL和SVG名列其中
- Eclipse即将发布基于浏览器的IDE
- 使用HTML5画布实现的超棒javascript动画仪表板:gauge.js
- 火狐封杀Silverlight和Java插件提升安全性
- CSS3圆角的实现教程
- 瀑布流多列布局排版控制CSS3的实现方法
- CSS渐变色效果的实现方法与效果演示
- jQuery 关于点击菜单项,使子条目“向上”展开效果的实现
- 原生体验挡不住!JavaScript开源跨平台框架NativeScript
- JavaScript开源跨平台框架NativeScript