Core JavaScript 1.5 Guide:Creating New Objects:Defining Getters and Setters
From MDC
Getter是一种获取一个属性的值的方法,setter是一种设定一个属性的值的方法。可以为任何预定义的核心对象或用户自定义对象定义getter和setter方法,从而为现有的对象添加新的属性。定义Getters和Setters 的语法使用的是对象文本化语法。
下面的这段JS代码介绍了如何在一个用户自定义的对象o上使用getter和setter。JS Shell是一个JS的调试工具,开发人员可以在上面批量测试代码,或是进行交互测试。
The o object's properties are: 对象“o”的属性如下。
- o.a - 一个数字
- o.b - 一个getter 返回比o.a大1的值
- o.c - 一个setter 设置o.a的值为原来的一半
o = new Object; o = {a:7, get b() {return this.a+1; }, set c(x) {this.a = x/2}}; o.a o.b o.c = 50 o.a
下面的JS代码介绍了如何使用getter和setter来扩展Date原型,从而为预定义类型Date的所有实例都添加一个年的属性。它使用了Date类中原有的getFullYear和setFullYear方法来实现年(year)属性的getter和setter.
下面的语句定义了属性“年”(year)的getter和setter: var d = Date.prototype; d.__defineGetter__("year", function() { return this.getFullYear(); }); d.__defineSetter__("year", function(y) { this.setFullYear(y); });
下面的语句使用了Date对象的getter和setter方法 var now = new Date; alert(now.year); now.year=2001; alert(now);
在Javascript 1.5的开发过程中,有过很小的一段时间,可以在表达式里用“getter=”或是“setter=”这种方式来对已存在的对象定义新的getter和setter。这种语法现在已经废弃,将会在JS1.5的引擎中产生一个警告waring,将来会成为一个错误error。应该避免这样的写法。
摘要
原则上getters 和 setters 都可以
- 使用对象初始化过程来定义,或者
- 在之后的任何时候,使用添加getter/setter的方法来追加定义。
在使用对象初始化过程来定义getter和setter时,唯一要做的事就是在getter方法的前面加上 "get",在setter方法的前面加上"set"。当然了,getter方法是没有参数的,而setter方法明确的要求有一个参数(设置的新值)。例如:
o = {
a:7,
get b() { return this.a+1; },
set c(x) { this.a = x/2; }
};
在对象创建之后的任何时候,还可以通过两个特殊的方法来为它添加getter和setter,这两个方法是"__definedGtter__"和"__definedSetter__"。这两个方法都要求它们的第一个参数是getter或setter的名字,以string的形式给出;第二个参数是一个作为getter或是setter的函数function.例如(续上例)
o.__defineGetter__("b", function() { return this.a+1; }); o.__defineSetter__("c", function(x) { this.a = x/2; });
使用哪种形式,取决个人的编程风格和当前的任务。如果在原型定义的时候已经考虑对象初始化过程,大多数情况下都选择第一种形式,这种形式更紧凑,也更加自然。当然了,如果需要以后添加getter和setter--因为这个原型或是特定的对象并不是你写的--就只能用后一种形式了。第二种形式可能更好的体现了JS的动态特征-这也使得代码难以阅读和理解。
A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property. You can define getters and setters on any predefined core object or user-defined object that supports the addition of new properties. The syntax for defining getters and setters uses the object literal syntax.
Getter是一种获取一个属性的值的方法,setter是一种设定一个属性的值的方法。可以为任何预定义的核心对象或用户自定义对象定义getter和setter,从而为现有的对象添加新的属性。定义Getters和Setters 的语法使用的是对象[文本化语法][1]。
The following JS shell session illustrates how getters and setters could work for a user-defined object o. The JS shell is an application that allows developers to test JavaScript code in batch mode or interactively. 这段JS代码很好的说明了开发人员如何使用Getters和Setters定义对象“o”。开发人员可以在环境下进行调试。
The o object's properties are: 对象“o”的原型如下。
- o.a - a number
一个数字
- o.b - a getter that returns o.a plus 1
返回o.a加1的值
* o.c - a setter that sets the value of o.a to half of its value
设定o.a的值为o.c的一半
o = new Object; o = {a:7, get b() {return this.a+1; }, set c(x) {this.a = x/2}}; o.a o.b o.c = 50 o.a
This JavaScript shell session illustrates how getters and setters can extend the Date prototype to add a year property to all instances of the predefined Date class. It uses the Date class's existing getFullYear and setFullYear methods to support the year property's getter and setter. 这个代码向我们演示了如何使用Getters和Setters继承原型中的数据,然后添加一个 “year”原型成为这个日期类的一个实例。其中使用到了日期类中已有的两个方法:getFullYear 和 setFullYear。
These statements define a getter and setter for the year property: 以下代码使用getter 和 setter声明了一个“year”原型:
var d = Date.prototype; d.__defineGetter__("year", function() { return this.getFullYear(); }); d.__defineSetter__("year", function(y) { this.setFullYear(y); });
These statements use the getter and setter in a Date object: 以下代码使用getter 和 sette操作这个类对象。
var now = new Date; alert(now.year); now.year=2001; alert(now);
During development of JavaScript 1.5, there was a brief period in which expressions including getter = or setter = were used to define new getters or setters on existing objects. This syntax is highly deprecated now, will cause a warning in current JS 1.5 engines, and will become a syntax error in the future. It should be avoided. 在JavaScript 1.5的开发过程中,有一种简短的使用类似getter = 或者 setter = 定义对象的方法。这样的语法在JavaScript 1.5版本中会获得一个警告,以后的版本也不会支持,所以我们要避免这样的语法出现。
Summary 摘要
In principle, getters and setters can be either 原则上getters 和 setters 都可以实现
- defined using object initializers, or
定义使用初始化对象
* added later to any object at any time using a getter or setter adding method.
给现有的对象添加新的方法
When defining getters and setters using object initializers all you need to do is to prefix a getter method with get and a setter method with set. Of course, the getter method must not expect a parameter, while the setter method expects exactly one parameter (the new value to set). For instance: 定义使用的初始化对象只需要在语句前加上get和set分别实现getter方法和setter方法。当然,getter方法不能获取到任何参数,而sette则需要给予一个参数(需要新设定的值)。距离如下:
o = {
a:7,
get b() { return this.a+1; },
set c(x) { this.a = x/2; }
};
Getters and setters can also be added to an object at any time after creation using two special methods called __defineGetter__ and __defineSetter__. Both methods expect the name of the getter or setter as their first parameter, in form of a string. The second parameter is the function to call as the getter or setter. For instance (following the previous example): Getters 和 setters还可以通过 __defineGetter__ 和 __defineSetter__ 这两个特殊的方法实现对对象的添加。这两种方法都需要把方法的名字作为第一个字符串形式的参数,第二个参数是getter 和 setter需要调用的函数,举例如下(和上面的例子功能相同):
o.__defineGetter__("b", function() { return this.a+1; }); o.__defineSetter__("c", function(x) { this.a = x/2; });
Which of the two forms to choose depends on your programming style and task at hand. If you already go for the object initializer when defining a prototype you will probably most of the time choose the first form. This form is more compact and natural. However, if you need to add getters and setters later – because you did not write the prototype or particular object – then the second form is the only possible form. The second form probably best represents the dynamic nature of JavaScript – but it can make the code hard to read and understand. 使用哪一种方式取决于你项目的形式和需要实现的功能。如果你在定义一个原型时需要初始化一个对象,一般都会选择第一种,形式会更加自然。如果你并不急着需要使用这两个方法(并不初始化原型和对象),那么只能使用第二种方法。第二种方法更好的体现出了JS语法的特点,但是有时候也会使代码变得难以阅读和理解。