使用Object.defineProperty() 定义对象属性时,如已设置 set 或 get, 就不能设置 writable 和 value 中的任何一个了,不然会报如下错误。
TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>
let obj_test = { name:'abc' }; Object.defineProperty(obj_test, "age", { // value:18, // writable:true, enumerable:true, configurable:true, set:function(new_age){ console.log('设置age属性...') num = new_age+1; }, get:function(){ console.log('读取age属性...') return num; } }) console.log(obj_test)
标签:set,age,Object,value,writable,设置,defineProperty From: https://www.cnblogs.com/lifengjuan/p/17722573.html