简体   繁体   English

在文字对象中创建属性

[英]Creating properties in literal object

var o, d;

o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, "foo");
// d is { configurable: true, enumerable: true, get: /*the getter function*/, set:     undefined }

What does that get within the object do? 是什么get对象做内? Is that a method or property or something else? 这是方法或财产还是其他什么? How does it work or how it set property or method to object? 它如何工作或如何设置属性或方法来对象? Will i fall into trouble if i simply ignore the use of get and set ?Are there more advantages in using get and set than simply defining property without there use.What are those advantages if any.Also what will the .getOwnPropertyDescriptor() method returns? 如果我简单地忽略getset的使用,我会遇到麻烦吗?使用getset比使用get使用简单定义属性更有优势。如果有的话有什么优势。还有.getOwnPropertyDescriptor()方法返回什么? If it returns object, can I simply do returnedobj.configurable to access the configurable property-value? 如果它返回对象,我可以简单地使用returnedobj.configurable来访问可配置的属性值吗?

The get defines a property accessor function. get定义了属性访问器函数。 When the value of the foo property on o is retrieved, that function is called even though it doesn't look like a function call in the code, eg: 当检索到o上的foo属性的值时,即使它看起来不像代码中的函数调用,也会调用该函数,例如:

var a = o.foo; // Note that `foo` doesn't have () after it, it's not a function call

In this case, it always returns 17, but it could do something else instead. 在这种情况下,它总是返回17,但它可以做其他事情。 For instance, consider a circle: 例如,考虑一个圆圈:

var circle = {
    radius: 7,
    get circumference() { return 2 * Math.PI * this.radius; },
    get area()          { return Math.PI * this.radius * this.radius; }
};
console.log(circle.circumference); // 43.982297150257104 
console.log(circle.area);          // 153.93804002589985 
circle.radius = 4;
console.log(circle.circumference); // 25.132741228718345
console.log(circle.area);          // 50.26548245743669 

As you can see, when we access the two properties we defined with accessors, the functions assigned to them get called, even though the property access doesn't look like a function call. 如您所见,当我们访问我们使用访问器定义的两个属性时,即使属性访问看起来不像函数调用,也会调用分配给它们的函数。

You can also have functions that get called when the property is set . 您还可以设置在设置属性时调用的函数。 Unsurprisingly, you do that using set rather than get . 不出所料,你使用set而不是get来做到这一点。 :-) :-)

You can read more about this in the object initializers part of the specification, and on MDN . 您可以在规范的对象初始化器部分和MDN上阅读有关此内容的更多信息。

The Object.getOwnPropertyDescriptor call returns an object that describes the property you asked for (in this case, foo ). Object.getOwnPropertyDescriptor调用返回一个描述您要求的属性的对象(在本例中为foo )。 You can read more about it in the spec and on MDN as well. 您可以在规范MDN中阅读更多相关信息。

Quoting from MDN: 引自MDN:

A property descriptor is a record (TJC: eg, object) with some of the following attributes: 属性描述符是具有以下某些属性的记录(TJC:例如,对象)

value
The value associated with the property (data descriptors only). 与属性关联的值(仅限数据描述符)。
writable
true if and only if the value associated with the property may be changed (data descriptors only). 当且仅当与属性关联的值可能更改时才为true (仅限数据描述符)。
get
A function which serves as a getter for the property, or undefined if there is no getter (accessor descriptors only). 用作属性的getter的函数,如果没有getter则不undefined (仅限访问者描述符)。
set
A function which serves as a setter for the property, or undefined if there is no setter (accessor descriptors only). 一个函数,用作属性的setter,如果没有setter,则为undefined (仅限访问者描述符)。
configurable
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. 当且仅当可以更改此属性描述符的类型并且可以从相应对象中删除属性时,才返回true
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object. 当且仅当在枚举相应对象的属性期间显示此属性时,才返回true

get is part of the ECMAScript 5 syntax for defining property getters and setters. get是ECMAScript 5语法的一部分,用于定义属性getter和setter。

Using object literal syntax, it's defined like this: 使用对象文字语法,它定义如下:

var obj = {
    __other_property__: null,
    get foo() { return "bar"; },
    set foo(v) { this.__other_property__ = v }
};

This lets you invoke the body of the getter function when doing a get on the property. 这使您可以在获取属性时调用getter函数的主体。

obj.foo = "oof";
console.log(obj.foo); // "bar"
console.log(obj.__other_property__); // "oof"

The above uses foo to set a different property __other_property__ . 以上使用foo设置不同的属性__other_property__ This could be a local variable or something else, and the functions can obviously do far more complex operations than I'm showing here. 这可能是一个局部变量或其他东西,这些函数显然可以做比我在这里展示的更复杂的操作。

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

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