ES6新增对象方法的访问描述符:get(只读)、set(只写)

Es6新增对象方法的访问描述符:get(只读)、set(只写),可以直接使用,一般用于数据监听,用途类似于vue.$watch。

var obj = {

a:1,

get bar() { return this.a},

set bar(a) { this.a = a; return this.a }

}

obj.bar //1

obj.bar = 2

obj.bar //2

猜你喜欢

转载自www.cnblogs.com/robint/p/10297890.html