在 javascript 中使用 setter 或方法之间的区别
difference between using setters or methods in javascript
我正在阅读 javascript 中的 getter 和 setter。我想知道这两种使用和不使用 setter 的编码方式是否有区别
第一种方式,没有二传手。
>obj1 = {
arr: [];
}
>obj1.arr.push('first')
>obj1.arr
[ 'first' ]
第二种方式,使用 setter。
>obj2 = {
set add(data) {
this.arr.push(data);
},
arr: []
}
>obj2.add = 'first'
>obj2.arr
[ 'first' ]
首先,set
语法将对象 属性 绑定到定义的函数。在此特定示例中,两个代码之间没有区别,但是假设您想在将值添加到数组之前检查该值是否为负,因此您可以使用 set to Encapsulate[=22= 】 那种行为。
所以基本上,使用setter只是为对象的功能添加额外的封装行为。
访问数组索引的方式称为括号表示法。它等同于圆点表示法,除了括号表示法允许您动态地为对象或数组设置新属性。
希望对您有所帮助。
我觉得区别只有"how it looks like"。对于从面向对象语言来到 js 的人来说,使用 setter 是最接近理解的方式。
您的示例中的 setter 语法并没有真正阻止客户端代码仍然使用直接 push
调用添加一个值,就像在第一个代码块中一样。所以区别在于你只是添加了另一种方法来做同样的事情。
为了进行公平比较,您必须在两种备选方案中定义 相同的 方法:一次作为普通方法,一次作为 setter 方法,并且那么区别只是语法如何将参数传递给方法,使用 obj.add('first')
或 obj.add = 'first'
.
在这种实际情况下,我会投票反对 setter,因为它给人一种错误的印象,即如果您 "assign" 另一个值,第一个分配的值将 覆盖 :
obj.add = 'first';
obj.add = 'second';
...但显然情况并非如此:这两个值现在都存在于对象中。
getter/setter属性和"normal"实例属性不一样,一个叫"named data property",一个叫"named accessor property".
请让 met 引用 ECMAScript 5.1 中的以下部分文档。
https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1
An Object is a collection of properties. Each property is either a
named data property, a named accessor property, or an internal
property:
A named data property associates a name with an ECMAScript language
value and a set of Boolean attributes.
A named accessor property associates a name with one or two accessor
functions, and a set of Boolean attributes. The accessor functions are
used to store or retrieve an ECMAScript language value that is
associated with the property.
An internal property has no name and is not directly accessible via
ECMAScript language operators. Internal properties exist purely for
specification purposes.
There are two kinds of access for named (non-internal) properties: get
and put, corresponding to retrieval and assignment, respectively.
和
If the value of an attribute is not explicitly specified by this
specification for a named property, the default value defined in Table
7 is used.
我正在阅读 javascript 中的 getter 和 setter。我想知道这两种使用和不使用 setter 的编码方式是否有区别
第一种方式,没有二传手。
>obj1 = {
arr: [];
}
>obj1.arr.push('first')
>obj1.arr
[ 'first' ]
第二种方式,使用 setter。
>obj2 = {
set add(data) {
this.arr.push(data);
},
arr: []
}
>obj2.add = 'first'
>obj2.arr
[ 'first' ]
首先,set
语法将对象 属性 绑定到定义的函数。在此特定示例中,两个代码之间没有区别,但是假设您想在将值添加到数组之前检查该值是否为负,因此您可以使用 set to Encapsulate[=22= 】 那种行为。
所以基本上,使用setter只是为对象的功能添加额外的封装行为。
访问数组索引的方式称为括号表示法。它等同于圆点表示法,除了括号表示法允许您动态地为对象或数组设置新属性。
希望对您有所帮助。
我觉得区别只有"how it looks like"。对于从面向对象语言来到 js 的人来说,使用 setter 是最接近理解的方式。
您的示例中的 setter 语法并没有真正阻止客户端代码仍然使用直接 push
调用添加一个值,就像在第一个代码块中一样。所以区别在于你只是添加了另一种方法来做同样的事情。
为了进行公平比较,您必须在两种备选方案中定义 相同的 方法:一次作为普通方法,一次作为 setter 方法,并且那么区别只是语法如何将参数传递给方法,使用 obj.add('first')
或 obj.add = 'first'
.
在这种实际情况下,我会投票反对 setter,因为它给人一种错误的印象,即如果您 "assign" 另一个值,第一个分配的值将 覆盖 :
obj.add = 'first';
obj.add = 'second';
...但显然情况并非如此:这两个值现在都存在于对象中。
getter/setter属性和"normal"实例属性不一样,一个叫"named data property",一个叫"named accessor property".
请让 met 引用 ECMAScript 5.1 中的以下部分文档。
https://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1
An Object is a collection of properties. Each property is either a named data property, a named accessor property, or an internal property:
A named data property associates a name with an ECMAScript language value and a set of Boolean attributes.
A named accessor property associates a name with one or two accessor functions, and a set of Boolean attributes. The accessor functions are used to store or retrieve an ECMAScript language value that is associated with the property.
An internal property has no name and is not directly accessible via ECMAScript language operators. Internal properties exist purely for specification purposes.
There are two kinds of access for named (non-internal) properties: get and put, corresponding to retrieval and assignment, respectively.
和
If the value of an attribute is not explicitly specified by this specification for a named property, the default value defined in Table 7 is used.