我不理解Objects的可写和可配置属性属性 [英] I don't understand writable and configurable property attributes of Objects

查看:86
本文介绍了我不理解Objects的可写和可配置属性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不理解Objects的Writable和Configurable属性。例如,在 Object.prototype <的MDN中/ a>,有一个表格,我可以清楚地看到 Object.prototype 的可配置,可写和可枚举的属性属性被锁定。

I don't understand the Writable and Configurable attributes of Objects. For example, in the MDN for Object.prototype, there is a table where I can clearly see that Configurable, Writable and Enumerable Property Attributes of Object.prototype are locked.

但是,我可以编写和扩展Object.prototype,例如使用以下代码:

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


推荐答案

MDN所指的是属性原型 对象本身。您不能覆盖 Object.prototype 本身。如果您尝试使 Object.prototype 未定义,那将失败:

What the MDN is referring to is the property prototype of Object itself. You cannot overwrite Object.prototype itself. If you try and make Object.prototype undefined, that will fail:

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

如果您在严格模式下尝试此操作,您将获得<$ c尝试分配给不可写属性时$ c> TypeError :

If you try this in strict mode, you will get a TypeError upon attempting to assign to a non-writable property:

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

您可以在不更改对象引用的内容的情况下写入对象自己的属性,并且这些属性具有单独的属性。例如,请看:

You can write to an object's own properties without changing what the object's reference is, and those 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

有一个单独的 [[Extensible]] 阻止在对象上创建新属性的内部属性 - 如果调用 Object.preventExtensions,则设置为 false code>, Object.seal Object.freeze

There is a separate [[Extensible]] internal property that prevents the creation of new properties on an object -- this is set to false if you call Object.preventExtensions, Object.seal or Object.freeze.

请注意,在对象上调用 Object.freeze 不是一个好主意.prototype ,因为真的很奇怪:

Note that it's not a good idea to call Object.freeze on something like Object.prototype, as really weird 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] } 

就像前面的例子一样,它也会在严格模式下抛出 TypeError

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

基本上,即使它是对象本身的属性,它也是使用原型链中的属性来检查它是否可以分配属性。一些人用语言认为是一个错误但是其他人认为这种行为是设计的。

Basically, even though it would be a property on the object itself, it uses the attributes from the prototype chain to check whether or not it can assign the property. This has been considered as a mistake in the language by some people, however others consider this behaviour to be by design.

这篇关于我不理解Objects的可写和可配置属性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆