I'm so sorry.这篇文章竟然是我的疑问篇,这是今天在使用Object.defineProperty()方法的时候遇到的问题。

At first Object.defineProperty是为了解决我们在js中不能直接访问ECMA-262定义的数据属性和访问器属性,因为这些属性是

为了实现JavaScript引擎用的。

Qusetion one:

like the following:

关于在new Object()后,使用Object.defineProperty()修改了这个对象之后,有什么影响?

brief summary:

Could you help me?

Question two:

var o  = new Object();
Object.defineProperty(o,"age",{//数据属性
  configurable:true,
  writable:true,//这行一定要显式定义为true,因为只要用了Object.defineProperty()则所有数据属性的值defaults false
  value:30
})
//o.age = 30;
Object.defineProperty(o,"visitAge",{//访问器属性
  get:function(){
    return this.age;
  },
  set:function(newValue){
    this.age = newValue;
  }
})
o.visitAge = 10;

console.log(o.age);//10 //上面的关键是将writable设置为true,如果是o.age = 30的形式,则不用显式定义writable:true
console.log(o.age);//30 //未将writable设置为true,则访问器属性无法将age=10

brief summary:

point one:理解对象在调用Object.defineProperty(o,"property",{datas or visitor}) 未显式申明时,其默认值是false。


相关文章:

  • 2021-11-03
  • 2021-09-02
  • 2021-08-07
  • 2022-12-23
  • 2021-06-20
  • 2021-07-14
  • 2022-12-23
  • 2021-12-15
猜你喜欢
  • 2021-06-03
  • 2022-01-03
  • 2021-10-22
  • 2021-11-11
  • 2021-11-03
  • 2021-07-23
相关资源
相似解决方案