龙空技术网

我的JavaScript学习笔记:读取属性的特性

JSindexHTML 84

前言:

此时咱们对“js获取属性值”大概比较珍视,我们都想要知道一些“js获取属性值”的相关知识。那么小编同时在网络上收集了一些对于“js获取属性值””的相关资讯,希望朋友们能喜欢,咱们一起来了解一下吧!

我们使用Object.getOwnPropertyDescription()获取给定属性的描述符。

语法:Object.getOwnPropertyDescription(obj,prop)

obj是属性所在的对象,prop是读取其描述符的属性名称。

let book = {};        Object.defineProperties(book, {            _year: { value: 2019 },            edition: { value: 1 },            year: {                get: function () {                    return this._year                },                set: function (newValue) {                    if (newValue > 2019) {                        this._year = newValue;                        this.edition += newValue - 2019                    }                }            }        });let descriptor=Object.getOwnPropertyDescriptor(book,"_year");console.log(descriptor.value);//2019console.log(typeof descriptor.get);//undefinedconsole.log(descriptor.configurable);//falselet descriptor=Object.getOwnPropertyDescriptor(book,"year");console.log(descriptor.value);//undefinedconsole.log(descriptor.eunmerable);//falseconsole.log(typeof descriptor.get);//function

对于数据属性_year,value 等于最初的值,configurable 是 false,而 get 等于 undefined。 对于访问器属性 year,value 等于 undefined,enumerable 是 false,而 get 是一个指向 getter 函数的指针。

标签: #js获取属性值