美文网首页
object深度解析

object深度解析

作者: saltfish666 | 来源:发表于2018-11-27 18:11 被阅读0次

我们知道JS是面对对象的编程语言,但其实很多人对JS中的对象没有足够的理解。这次我们从ES规范入手,深入的理解object。

我们来看看ES规范是如何定义object的:

  • An Object is logically a collection of properties.
  • Each property is either a data property, or an accessor property
    • A data property associates a key value with an ECMAScript language value and a set of Boolean attributes.
    • An accessor property associates a key value with one or two accessor functions, and a set of Boolean attributes. The accessor functions are used to store or retrieve an ECMAScript language value that is associated with the property.

可以看到,一个object就是由一个个属性(property)以及属性描述符(attributes)组成的。而属性又分为两种,数据属性(data property)和访问属性( accessor property)。
比如说上述的obj1,它就有两个数据属性a b,他们的值分别为1 2。那它的属性描述符是什么呢?

截图自ECMAScript
我们可以再JS代码中这样查看:
var obj1 = {
    a: 1,
    b: 2
}
console.log(Object.getOwnPropertyDescriptor(obj1, 'a'))
{ value: 1, writable: true, enumerable: true, configurable: true }

这就是对象的全部面貌吗?好有很多

innermethod

相关文章

网友评论

      本文标题:object深度解析

      本文链接:https://www.haomeiwen.com/subject/vfwiqqtx.html