![](/img/trans.png)
[英]Is there any difference between WeakMap and private member of a class in JavaScript ES6?
[英]private props in ES6 don't work with WeakMap
我創建了以下課程:
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
道具私有化,自ES6開始就沒有機會使用私有道具了。 不幸的是,使用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) });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.