Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

javascript - 访问器属性如何在Javascript中运行?(How do accessor properties work inside in Javascript?)

I have noticed that using accessor properties instead of data properties in js then the object is forced to store the value of the property twice:

(我注意到在js中使用访问器属性而不是数据属性,然后该对象被强制存储两次该属性的值:)

let obj = {
  get propName(){
    return this._propName;
  },
  set propName(value){
   this._propName = value;
  }
}

obj.propName = "some_value";

for(let key in obj){
  if((obj.hasOwnProperty(key)) && (typeof key != 'function')){
    console.log(key + ": " + obj[key])
  }
}

The output is:

(输出为:)

propName: some_value

_propName: some_value

I am right?

(我是正确的?)

  ask by Arayik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...