Difference between this property descriptor and property assignment in ECMAScript 5?

I read a little more about ECMAScript 5 (in a browser using ES5-shim , which, as I know, does not support everything). And just to eliminate any confusion, given that I have this object (stolen from this post ):

var Person = { firstName: null, // the person's first name lastName: null // the person's last name }; 

Is there any difference between this:

 var Employee = Object.create(Person, { id: { value: null, enumerable: true, configurable: true, writable: true } }); 

And this:

 var Employee = Object.create(Person); Employee.id = null; // Or using jQuery.extend $.extend(Employee, { id : null }); 

As I understand it, enumerated, custom, and writable can be set to true when defining a property this way (which will also be backward compatible with legacy JavaScript mechanisms). Am I missing something or can I just omit the descriptors of the detailed properties whenever I want this to be the desired behavior?

+4
source share
1 answer

They are the same.

When creating new properties by assigning

 obj.prop = val; 

all three logical attributes of the newly created property are set to true .


Also note that when adding properties through Object.defineProperty

 Object.defineProperty( obj, 'prop', { value: val }); 

Boolean attributes are set to false (default).

+4
source

Source: https://habr.com/ru/post/1392271/


All Articles