Object.getOwnPropertyDescriptor(obj, prop)
obj
prop
如果指定的属性存在于对象上,则返回其属性描述符对象(property descriptor),否则返回 undefined
。
该方法允许对一个属性的描述进行检索。在 Javascript 中, 属性 由一个字符串类型的“名字”(name)和一个“属性描述符”(property descriptor)对象构成。更多关于属性描述符类型以及他们属性的信息可以查看:Object.defineProperty
.
一个属性描述符是一个记录,由下面属性当中的某些组成的:
value
writable
当且仅当属性的值可以被改变时为true。(仅针对数据属性描述有效)
get
set
configurable
当且仅当指定对象的属性描述可以被改变或者属性可被删除时,为true。
enumerable
true
。var o, d; o = { get foo() { return 17; } }; d = Object.getOwnPropertyDescriptor(o, "foo"); // d { // configurable: true, // enumerable: true, // get: /*the getter function*/, // set: undefined // } o = { bar: 42 }; d = Object.getOwnPropertyDescriptor(o, "bar"); // d { // configurable: true, // enumerable: true, // value: 42, // writable: true // } o = {}; Object.defineProperty(o, "baz", { value: 8675309, writable: false, enumerable: false }); d = Object.getOwnPropertyDescriptor(o, "baz"); // d { // value: 8675309, // writable: false, // enumerable: false, // configurable: false // }
在 ES5 中,如果该方法的第一个参数不是对象(而是原始类型),那么就会产生出现 TypeError
。而在 ES2015,第一个的参数不是对象的话就会被强制转换为对象。
原文:https://www.cnblogs.com/wyongz/p/11535923.html