Javascript对象属性? [英] Javascript object attributes?
问题描述
我知道所有对象属性都有一个名称,并且具有 value
,可配置
,<$ c $等属性c>可枚举和可写
。但在这篇文章中,我读到的对象也有像 prototype
, class
和可扩展
。
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
.
我知道 prototype
属性用于指向父对象。但我不明白的是 class
属性是什么?有这样的属性吗?并且不是可扩展
是一个对象的方法,因为 isExtensible()
?
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
和可扩展
。
它们通常不被称为属性,但 内部广告位 。通常它们用双括号表示,以区别于正常属性,即[[prototype]],[[class]]和[[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]].
什么是[[class]]属性?是否有这样的属性?
What is the [[class]] attribute? Is there such attribute?
不再在ES6中了。 [[class]]内部插槽包含有关该对象的内置类型(例如,Array,RegExp,内置包装器)的信息。在对象上使用 Object.prototype.toString
方法时显示。 (看看为什么Object.prototype.toString.call(foo)可以检测到foo的类型?或为什么`Object.prototype.toString`总是返回`[object *]`?以获取更多细节 - 这也是检测的最佳方式在 Array.isArray
可用之前,对象是否是数组。)
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).
从ES6开始,没有这样的内部插槽以及 Object.prototype.toString
依赖于 Symbol.toStringTag
机制。
Since ES6, there is no such internal slot any more and Object.prototype.toString
relies on the Symbol.toStringTag
mechanism now.
并且不能将对象的方法扩展为isExtensible()?
And isn't extensible a method of object as isExtensible()?
不, [[extensible]]内部插槽是 isExtensible()
访问的东西,而 Object.preventExtensions()
可以集。
No, the [[extensible]] internal slot is the thing that isExtensible()
accesses, and that Object.preventExtensions()
can set.
这篇关于Javascript对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!