ES6 中的私有属性不适用于 WeakMap
private props in ES6 don't work with WeakMap
我创建了以下 class:
import Confidence from 'confidence';
import manifest from './manifest';
import criteria from './criteria';
const privateProps = new WeakMap();
class Configuration {
constructor() {
privateProps(this, { store: new Confidence.Store(manifest) });
}
getKey(key) {
return privateProps.get(this).store.key(key, criteria);
}
getMeta(key) {
return privateProps.get(this).store.meta(key, criteria);
}
}
let configuration = new Configuration();
export default configuration;
为了使 store
props 私有,因为在 ES6 中到目前为止还没有机会拥有私有 props。不幸的是用 babel 转换我得到这个错误:
privateProps(this, { store: new _confidence2['default'].Store(_manifes
TypeError: object is not a function
知道错在哪里吗?
如错误所述,privateProps
(WeakMap
实例)不是函数。
你的意思可能是:
privateProps.set(this, { store: new Confidence.Store(manifest) });
我创建了以下 class:
import Confidence from 'confidence';
import manifest from './manifest';
import criteria from './criteria';
const privateProps = new WeakMap();
class Configuration {
constructor() {
privateProps(this, { store: new Confidence.Store(manifest) });
}
getKey(key) {
return privateProps.get(this).store.key(key, criteria);
}
getMeta(key) {
return privateProps.get(this).store.meta(key, criteria);
}
}
let configuration = new Configuration();
export default configuration;
为了使 store
props 私有,因为在 ES6 中到目前为止还没有机会拥有私有 props。不幸的是用 babel 转换我得到这个错误:
privateProps(this, { store: new _confidence2['default'].Store(_manifes
TypeError: object is not a function
知道错在哪里吗?
如错误所述,privateProps
(WeakMap
实例)不是函数。
你的意思可能是:
privateProps.set(this, { store: new Confidence.Store(manifest) });