我知道所有的对象属性都有一个名称,并且具有诸如value ,可configurable , enumerable和writable属性。 但在这篇文章中,我读到的对象也有像prototype , class和extensible属性。
我明白prototype属性是用于指向父对象的。 但是我不明白的是什么是class属性? 有这样的属性吗? isExtensible()是不是extensible的对象方法?
I know that all object properties have a name and have attributes like value, configurable, enumerable and writable. But in this post I read that objects too have attributes like prototype, class and extensible.
I understand that prototype attribute is for pointing for the parent object. But what I don't understand is what's class attribute? Is there such attribute? And isn't extensible is a method of object as isExtensible() ?
最满意答案
在这篇文章中,我读到对象也具有类似prototype , class和extensible属性。
它们通常不被称为“属性”,而是内部插槽 。 通常它们用双括号表示以区分它们与正常属性,即[[prototype]],[[class]]和[[extensible]]。
什么是[[class]]属性? 有这样的属性吗?
不再在ES6中。 [[class]]内部插槽包含关于对象是哪种内置类型(例如Array,RegExp,内置包装器)的信息。 它在对象上使用Object.prototype.toString方法时显示。 (看看为什么Object.prototype.toString.call(foo)可以检测foo的类型?或者为什么Object.prototype.toString总是返回`[object *]`?以获取更多细节 - 这也是最好的方法在Array.isArray可用之前检测对象是否为数组)。
自ES6以来,不再有这样的内部槽,而Symbol.toStringTag现在依赖于Symbol.toStringTag机制。
并且isExtensible()不是可扩展的对象方法吗?
不,[[extensible]]内部插槽是isExtensible()访问的内容, Object.preventExtensions()可以设置。
In this post I read that objects too have attributes like prototype, class and extensible.
They're not called "attributes" normally but internal slots. Usually they are denoted by double brackets to differentiate them from normal properties, i.e. [[prototype]], [[class]] and [[extensible]].
What is the [[class]] attribute? Is there such attribute?
Not in ES6 any more. The [[class]] internal slot contained information on which kind of builtin type (e.g. Array, RegExp, builtin wrapper) the object was. It was shown when you used the Object.prototype.toString method on the object. (Have a look at Why can Object.prototype.toString.call(foo) detect foo's type? or Why does `Object.prototype.toString` always return `[object *]`? for more details - it was also the best way to detect whether an object is an array before Array.isArray was available).
Since ES6, there is no such internal slot any more and Object.prototype.toString relies on the Symbol.toStringTag mechanism now.
And isn't extensible a method of object as isExtensible()?
No, the [[extensible]] internal slot is the thing that isExtensible() accesses, and that Object.preventExtensions() can set.
更多推荐
发布评论