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

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

问题描述

我不了解对象的可写和可配置属性.例如,在MDN中,用于 Object.prototype,在一张表中,我可以清楚地看到 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); // 999

// Example 2
var o = {};
console.log(o.testing); // 999

推荐答案

MDN所指的是 Object 本身的属性 prototype .您不能覆盖 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]

如果您在严格模式下尝试使用此方法,则在尝试将其分配给不可写的属性时会得到 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]] 内部属性,可防止在对象上创建新属性-如果调用 false > Object.preventExtensions , 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.prototype 之类的对象调用 Object.freeze 并不是一个好主意,因为可能会发生奇怪的事情:

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.

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

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