在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ JS中的getter和setter函數(shù)

JS中的getter和setter函數(shù)

const person = {};

Object.defineProperty(person, 'age', {
  get: function() {
    //
  },
  set: function(newValue) {
    //
  }
});

person.age = 18;

console.log(person.age); // undefined

如上圖,在getset函數(shù)中,要如何寫才能正確的獲取和設(shè)置字段的值

回答
編輯回答
青裙
const person = {};

Object.defineProperty(person, 'age', {
  get: function() {
    return this.value
  },
  set: function(value) {
    this.value = value
  }
});

person.age = 18;

console.log(person.age); // 18
2017年3月29日 05:54