JS-getter和setter 发表于 2018-01-20 一、getter123{get prop() { ... } } {get [expression]() { ... } } 1234567var obj = { log: ['example','test'], get latest() { return this.log[this.log.length - 1]; }}console.log(obj.latest); // "test" 二、setter123{set prop(val) { . . . }}{set [expression](val) { . . . }} 12345678var language = { set current(name) { this.log.push(name); }, log: []}language.current = 'EN';console.log(language.log); // ['EN'] 三、注意1、不能将一个 getter或setter 绑定到一个具有真实值的属性上 2、使用delete操作符可以删除getter和setter