I do not understand what attributes of attributes of objects for recording and configuring

I do not understand the Writable and Configurable attributes for objects. For example, in MDN for Object.prototype there is a table in which I can clearly see that the Configurable, Writable and Enumerable Property Attributes of Object.prototype are locked.

However, I can write and extend Object.prototype, for example, with the following code:

// Example 1 Object.prototype.testing=999; console.log(Object.testing); // 9999 // Example 2 var o = {}; console.log(o.testing); // 9999 
+4
source share
4 answers

What MDN means is the prototype property of Object itself. You cannot overwrite Object.prototype . If you try to make Object.prototype undefined, this will not work:

 Object.prototype = 1; console.log(Object.prototype); // [object Object] 

If you try this in strict mode, you will get a TypeError when you try to assign a property that cannot be written:

 'use strict'; Object.prototype = 1; // TypeError: Cannot assign to read only property 'prototype' of function Object() { [native code] } 

You can write your own properties of the object without changing what the reference to the object is, and they have separate attributes. For example, see This:

 var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'toString'); console.log(descriptor.writable); // true console.log(descriptor.enumerable); // false console.log(descriptor.configurable); // true 

There is a separate internal [[Extensible]] property that prevents the creation of new object properties β€” this is false if you call Object.preventExtensions , Object.seal or Object.freeze .

Please note that it is not recommended to name Object.freeze for something like Object.prototype , since really strange things can happen:

 Object.freeze(Object.prototype); var building = {}; building.name = 'Alcove Apartments'; building.constructor = 'Meriton Apartments Pty Ltd'; console.log(building.constructor); // function Object() { [native code] } 

As in the previous example, it will also throw a TypeError in strict mode.

Basically, even if it is a property of the object itself, it uses the attributes from the prototype chain to check whether it can assign the property. This was considered a mistake in the language by some people, but others consider this design behavior.

+3
source

I can clearly see that the custom, writable, and enumerable property attributes of the Object.prototype object are locked. However, I can write Object.prototype .

No. Writeability only applies to the prototype property of an Object :

 Object.prototype = {}; // Error: Invalid assignment (in strict mode) // simply fails in lax mode 

And I can extend Object.prototype

Yes. You can extend the Object.prototype object (no matter how you relate to it); that another attribute (of an object, not a single property):

 var proto = Object.getPrototypeOf({}); proto.testing1 = 9999; // works Object.preventExtensions(proto); proto.testing2 = 9999; // Error: Invalid assignment (in strict mode) 
+2
source

From: http://ejohn.org/blog/ecmascript-5-objects-and-properties/

Writable: If false, the property value cannot be changed.

Configurable: if false, any attempt to delete a property or change its attributes (Writable, Configurable or Enumerable) will fail.

Enumerable: If true, the property will be repeated when the user executes (var prop in obj) {} (or similar).

+2
source

Variables, enumerated, and custom attributes in MDN seem to refer to the Object.prototype , and not to its properties.

So this means that you cannot replace Object.prototype with another object, but you are allowed to add properties to it.

So what does this mean if you do this:

 Object.prototype = {foo: "whatever"}; // doesn't work - is just ignored var j = {}; console.log(j.foo); // undefined 

Then the first line of code will do nothing.

+1
source

All Articles