Getter 对象的任意 属性
Getter for arbitrary property of object
我有一个 class 看起来像这样
export default class {
constructor () {
this.store = {}
}
setX (x, y) {
this.store[x] = y
}
}
如何在 this.store
上将 getter 定义为 return 0
当获得未定义的值时?
举个例子:
setX('a', 1)
会将 this.store['a']
设置为 1
然后 this.store['a']
会 return 1
,正如预期的那样。
但是 this.store['b']
会 return undefined
,但我希望 getter 改为 return 0
(也许调用 setX('b', 0)
,还不确定)。
我知道我可以使用 Object.defineProperty
来定义一个自定义的 getter,我只是想不通如何访问一个任意的、尚未定义的 属性 store
对象。
这完全可行还是我必须使用这样的解决方法?
getX (x) {
return this.store[x] || 0
}
我想避免这种情况,因为 this.store[x]
看起来干净多了。
How would I define a getter on this.store
to return 0
when getting an undefined
value?
除非你能预料到你想要支持的所有可能的 属性 名称并为它们定义 getter,否则你需要一个 Proxy with a get
trap,它是 ES2015 的新名称(并且不能被 polyfilled ).代理在性能方面很昂贵,只有在你真正需要它们时才使用它们。
示例:
class Example {
constructor () {
this.store = new Proxy({}, {
get(target, property) {
return property in target ? target[property] : 0;
}
});
}
setX (x, y) {
this.store[x] = y;
}
}
const e = new Example();
console.log("Setting a");
e.setX("a", "foo");
console.log("a = " + e.store.a);
console.log("b = " + e.store.b);
当然,如果您将 store
设为私有,则可以仅通过对象上的 getX
方法强制访问,这将避免使用代理,但要以定义 setX
和 getX
基于每个实例(现在,private data is coming):
class Example {
constructor () {
const store = {};
this.setX = (x, y) => {
store[x] = y;
};
this.getX = x => {
return x in store ? store[x] : 0;
};
}
}
const e = new Example();
console.log("Setting a");
e.setX("a", "foo");
console.log("a = " + e.getX("a"));
console.log("b = " + e.getX("b"));
我有一个 class 看起来像这样
export default class {
constructor () {
this.store = {}
}
setX (x, y) {
this.store[x] = y
}
}
如何在 this.store
上将 getter 定义为 return 0
当获得未定义的值时?
举个例子:
setX('a', 1)
会将 this.store['a']
设置为 1
然后 this.store['a']
会 return 1
,正如预期的那样。
但是 this.store['b']
会 return undefined
,但我希望 getter 改为 return 0
(也许调用 setX('b', 0)
,还不确定)。
我知道我可以使用 Object.defineProperty
来定义一个自定义的 getter,我只是想不通如何访问一个任意的、尚未定义的 属性 store
对象。
这完全可行还是我必须使用这样的解决方法?
getX (x) {
return this.store[x] || 0
}
我想避免这种情况,因为 this.store[x]
看起来干净多了。
How would I define a getter on
this.store
to return0
when getting anundefined
value?
除非你能预料到你想要支持的所有可能的 属性 名称并为它们定义 getter,否则你需要一个 Proxy with a get
trap,它是 ES2015 的新名称(并且不能被 polyfilled ).代理在性能方面很昂贵,只有在你真正需要它们时才使用它们。
示例:
class Example {
constructor () {
this.store = new Proxy({}, {
get(target, property) {
return property in target ? target[property] : 0;
}
});
}
setX (x, y) {
this.store[x] = y;
}
}
const e = new Example();
console.log("Setting a");
e.setX("a", "foo");
console.log("a = " + e.store.a);
console.log("b = " + e.store.b);
当然,如果您将 store
设为私有,则可以仅通过对象上的 getX
方法强制访问,这将避免使用代理,但要以定义 setX
和 getX
基于每个实例(现在,private data is coming):
class Example {
constructor () {
const store = {};
this.setX = (x, y) => {
store[x] = y;
};
this.getX = x => {
return x in store ? store[x] : 0;
};
}
}
const e = new Example();
console.log("Setting a");
e.setX("a", "foo");
console.log("a = " + e.getX("a"));
console.log("b = " + e.getX("b"));