访问器描述符:如何使用“get”并“设置”在实践中?
我不确定我是否做对了。
此示例直接来自 MDN(Mozilla 开发者网络):
var bValue;
Object.defineProperty(o, "b", {get : function(){ return bValue; },
set : function(newValue){ bValue = newValue; },
enumerable : true,
configurable : true});
发生的情况是 - 它创建了一个名为 bValue 的全局变量,但尚未完成。我知道这个例子只是演示了用法,因此如果它创建一个全局变量就可以了。但如果我要在应用程序中使用它,我会稍微修改一下,添加 this
关键字:
Object.defineProperty(o, "b", {get : function(){ return this.bValue; },
set : function(newValue){ this.bValue = newValue; },
enumerable : true,
configurable : true});
现在,对象 o
将具有属性 b
,同时它还会有另一个属性bValue
。用户(程序员)将仅暴露于“b”而不是“bValue”,尽管他仍然可以直接访问 bValue - 我不知道如何防止它。
我知道属性 b
和属性 bValue
可能并不总是相同,但 b
将取决于 bValue< 的值/code> 因为 getter 和 setter 允许我们在将值分配给
b
之前预处理 bValue。
主要问题是,我做对了吗?或者我在这里遗漏了什么?
I am not sure if I am getting it right.
This example is straight from MDN (Mozilla Developer Network):
var bValue;
Object.defineProperty(o, "b", {get : function(){ return bValue; },
set : function(newValue){ bValue = newValue; },
enumerable : true,
configurable : true});
What happens is - it creates a global variable named bValue, which is not done. I understand that this example only demonstrates the use and thus it is okay if it creates a global variable. But if I am going to use this in an application, I will modify it slightly, by adding the this
keyword:
Object.defineProperty(o, "b", {get : function(){ return this.bValue; },
set : function(newValue){ this.bValue = newValue; },
enumerable : true,
configurable : true});
Now, the object o
will have property b
, and at the same time, it will also have another property bValue
. The user (programmer) will be exposed only to 'b' and not to 'bValue' though he can still access bValue directly - I don't see how it can be prevented.
I understand that the property b
and the property bValue
may not always be same, but b
would depend on the value of bValue
because the getter and setter allow us to pre-process bValue before assigning the value to b
.
The main question is, am I getting it right? Or am I missing something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在寻找一个关闭。这是一种编码模式,使您能够使用私有变量并仅公开您想要公开的内容(公共变量)。
它创建一个函数并立即执行它。这似乎没用,但由于函数引入了一定程度的范围界定,因此除了在函数内以这种方式之外,无法在任何地方访问 bValue。
ob
属性充当开发人员和值之间的委托。您无法访问bValue
本身。 (尽管在此示例中,getter/setter 的行为显然与直接使用bValue
的行为完全相同。)http://jsfiddle.net/W4CSE/2/
You seem to be looking for a closure. This is a coding pattern that enables you to use private variables and only expose what you want to expose (public variables).
It creates a function and executes it immediately. This seems useless, but since functions introduce a level of scoping,
bValue
is not accessible anywhere but within the function this way.The
o.b
property acts as a delegate between the developer and the value. You cannot accessbValue
itself. (Though in this example the getter/setter obviously act such that they exactly do the same as usingbValue
directly.)http://jsfiddle.net/W4CSE/2/
这个想法是将代码包装在一个闭包中,从而将
bValue
隐藏起来,如下所示:现在您可以引用
ob
但不能引用bValue
The idea is to wrap the code in a closure, thus hiding
bValue
from the world, like this:now you can reference
o.b
but notbValue