js根据属性动态生成getter、setter方法
2009-03-22 15:06
846 查看
Object.prototype.setProperties = function(properties){
var obj = this;
// Iterate through the properties of the object, and make sure
// that it's properly scoped (as discussed previously)
for ( var i in properties ) {
(function(j){
// Create a new getter for the property
obj[ "get" + j ] = function() {
return properties[j];
};
// Create a new setter for the property
obj[ "set" + j ] = function(val) {
properties[j] = val;
};
})(i);
}
}
// Create a new user object that accepts an object of properties
function User( properties ) {
this.setProperties(properties);
}
// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
name: "Bob",
age: 44
});
// Just note that the name property does not exist, as it's private
// within the properties object
alert( user.name == null );
// However, we're able to access its value using the new getname()
// method, that was dynamically generated
alert( "name:" + user.getname() + ",age:" + user.getage());
// Finally, we can see that it''s possible to set and get the age using
// the newly generated functions
user.setage( 22 );
alert( user.getage() == 22 );
var obj = this;
// Iterate through the properties of the object, and make sure
// that it's properly scoped (as discussed previously)
for ( var i in properties ) {
(function(j){
// Create a new getter for the property
obj[ "get" + j ] = function() {
return properties[j];
};
// Create a new setter for the property
obj[ "set" + j ] = function(val) {
properties[j] = val;
};
})(i);
}
}
// Create a new user object that accepts an object of properties
function User( properties ) {
this.setProperties(properties);
}
// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
name: "Bob",
age: 44
});
// Just note that the name property does not exist, as it's private
// within the properties object
alert( user.name == null );
// However, we're able to access its value using the new getname()
// method, that was dynamically generated
alert( "name:" + user.getname() + ",age:" + user.getage());
// Finally, we can see that it''s possible to set and get the age using
// the newly generated functions
user.setage( 22 );
alert( user.getage() == 22 );
相关文章推荐
- js 匿名函数,动态生成setter,getter方法
- 属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter
- Eclipse 设置生成带有属性注释的getter/setter方法
- 浅谈 JS 对象添加 getter与 setter 的5种方法以及如何让对象属性不可配置或枚举
- 当把属性的语义特性分别声明为assign,retain,copy时, 自动生成setter, getter方法内部实现
- property生成属性的时候,同时重写setter与getter方法,那么实例变量不自动生成
- 快速生成setter与getter和该属性变量的方法(规范示例)
- javabean根据属性名称获取对应的的getter/setter方法名
- Objective-C,ios,属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter
- 玩转Eclipse — 自动生成setter和getter方法
- 使用原生js动态生成Select,根据前面的Select的option生成二级Select
- Eclipse中setter/getter方法自动添加属性注释
- JavaScript对象中属性的getter和setter方法
- js defineSetter -给js的 "class"自动增加一个set的属性(方法)
- 通过PropertyDescriptor反射获取属性的 getter/setter 方法
- 用工具生成的getter和setter方法的特殊情况
- js动态修改input输入框的type属性(实现方法解析)
- JS控制网页动态生成任意行列数表格的方法
- Angularjs根据json文件动态生成路由状态的实现方法
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法