简体   繁体   English

使用带有 Getter/Setter 的构造函数初始化 JavaScript/ES5 对象

[英]Initialization of a JavaScript/ES5 object using a constructor with Getter/Setter

The following constructor function is written in JavaScript / ES5 -以下构造函数是用 JavaScript / ES5 编写的 -

function Range(from, to) {

    function getFrom() { return from; }
    function getTo() { return to; }

    function setFrom(f) { from = f; }
    function setTo(t) { to = t; }

    Object.defineProperties(this, {
        fromProp: { get: getFrom, set: setFrom, enumerable: true, configurable: false },
        toProp: { get: getTo, set: setTo, enumerable: true, configurable: false }
    });
};

I create an object using this constructor as follows -我使用这个构造函数创建一个对象,如下所示 -

var range = new Range(4, 13);  

My general understanding of object creation is, there should be some code inside the constructor that, after instantiating the object range , will initialize fromProp and toProp with the values I passed through the parameters to the constructor.我对对象创建的一般理解是,构造函数内部应该有一些代码,在实例化对象range ,将使用我通过参数传递给构造函数的值初始化fromProptoProp But I'm having hard time understanding how exactly that is happening here.但我很难理解这里究竟是如何发生的。

Or, is it that the whole "initializing/accessing property" things here are captured inside the closure of setter/getter invocation?或者,这里的整个“初始化/访问属性”是否都在 setter/getter 调用的闭包中被捕获? But if so, then at any time when I use -但如果是这样,那么在我使用的任何时候——

range.fromProp = 22;

the value 22 actually never gets set to the range object's property fromProp , but rather to the parameter variable from , and then whenever I ask for -值 22 实际上从未设置为range对象的属性fromProp ,而是设置为参数变量from ,然后每当我要求时 -

var fromValue = range.fromProp;

it just hands me over the current value of the parameter variable from .它只是将参数变量的当前值from . Am I getting this right, or missing something?我做对了吗,还是遗漏了什么?

Any explanation on the matter?关于此事的任何解释?

function Range(from, to) {

function getFrom() { return from; }
function getTo() { return to; }

function setFrom(f) { from = f; }
function setTo(t) { to = t; }

Object.defineProperties(this, {
    fromProp: { get: getFrom, set: setFrom, enumerable: true, configurable: false },
    toProp: { get: getTo, set: setTo, enumerable: true, configurable: false }
});

}; };

Lets analyse here , you are setting the "get" property of "fromProp" as the reference of "getFrom" function where you are returning the value of "from" param and in the same way the "set" is referencing the function "setFrom" where the assignment of value to "from" is being done.让我们在这里分析一下,您将“fromProp”的“get”属性设置为“getFrom”函数的引用,您将在其中返回“from”参数的值,并且“set”以相同的方式引用函数“setFrom” " 正在完成将值分配给 "from" 的地方。

As the conclusion the get and set of fromProp is not directly holding the value rather it is holding the accessor function to return the value of "from".作为结论,fromProp 的 get 和 set 不直接保存值,而是保存访问器函数以返回“from”的值。

Hope your doubt is clear now.希望你的疑问现在清楚了。

If you are from a Java/C# world, then this of course seems a bit odd at first look, because in those languages any values passed through the constructor parameters are typically used to initialize the object, ie saving the values as the object's internal state, which are usually represented with private fields.如果您来自 Java/C# 世界,那么乍一看这当然有点奇怪,因为在这些语言中,通过构造函数参数传递的任何值通常用于初始化对象,即将值保存为对象的内部状态,通常用私有字段表示。 Then those private fields are exposed to the outside world via properties with getter/setter.然后这些私有字段通过带有 getter/setter 的属性暴露给外部世界。

So, the getter/setter in those languages actually encapsulate the object's internal data, not the constructor parameters.因此,这些语言中的 getter/setter 实际上封装了对象的内部数据,而不是构造函数参数。

JavaScript on the other hand does not provide the ability to make it's object's state private.另一方面,JavaScript 不提供将其对象的状态设为私有的能力。

That's why the values of the constructor parameters here have not been saved to any data properties, and rather, are captured in Closure of invocation of the accessor properties (as mentioned in the comments), to simulate the object's private state.这就是为什么这里构造函数参数的值没有保存到任何数据属性,而是在访问器属性调用的Closure中捕获(如注释中所述),以模拟对象的私有状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM