getter和setter
时间:2006-07-05
来源:互联网
firefox不支持innerText!
if(typeof HTMLElement!="undefined"){
HTMLElement.prototype.innerText
getter = function(){
var tmp = this.innerHTML.replace(/<br>/gi,"\n");
return tmp.replace(/<[^>]+>/g,"");
}
HTMLElement.prototype.innerText
setter = function(txtStr){
var parsedText = document.createTextNode(txtStr);
this.innerHTML = "";
this.appendChild( parsedText );
}
}
这段代码可以让firefox支持innerText
请问里面的getter和setter是哪里来的语法?
if(typeof HTMLElement!="undefined"){
HTMLElement.prototype.innerText
getter = function(){
var tmp = this.innerHTML.replace(/<br>/gi,"\n");
return tmp.replace(/<[^>]+>/g,"");
}
HTMLElement.prototype.innerText
setter = function(txtStr){
var parsedText = document.createTextNode(txtStr);
this.innerHTML = "";
this.appendChild( parsedText );
}
}
这段代码可以让firefox支持innerText
请问里面的getter和setter是哪里来的语法?
作者: samon127 发布时间: 2006-07-05
是自己定义的函数啊
作者: sycs 发布时间: 2006-07-05
getter,setter并不是我自己定义的函数名,我换成别的以后代码就不好用!
作者: samon127 发布时间: 2006-07-05
http://developer.mozilla.org/en/ ... Getters_and_Setters
不推荐使用的语法
will cause a warning in current JS 1.5 engines, and will become a syntax error in the future. It should be avoided.
不推荐使用的语法
will cause a warning in current JS 1.5 engines, and will become a syntax error in the future. It should be avoided.
作者: qileroro 发布时间: 2006-07-05
多谢qileroro,结帖!
作者: samon127 发布时间: 2006-07-06
Defining Getters and Setters
建立Getters和Setters
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是一种获取原型值的方法(method),setter是一种设定原型值的方法。开发人员可以获取和设定任何核心的对象(object)以及自定义的对象。使用Getters和Setters 的语法就和使用对象一样。
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语法的特点,但是有时候也会使代码变得难以阅读和理解。
建立Getters和Setters
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是一种获取原型值的方法(method),setter是一种设定原型值的方法。开发人员可以获取和设定任何核心的对象(object)以及自定义的对象。使用Getters和Setters 的语法就和使用对象一样。
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语法的特点,但是有时候也会使代码变得难以阅读和理解。
作者: samon127 发布时间: 2006-07-07
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28