Greasy Fork is available in English.
object with initialValue storing a weak reference to create private fields
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/391608/744027/PrivateFieldAccessor.js
// ==UserScript==
// @name PrivateFieldAccessor
// @namespace hoehleg.userscripts.private
// @version 0.1
// @description object with initialValue storing a weak reference to create private fields
// @author Gerrit Höhle
// @grant none
// ==/UserScript==
/* jshint esversion: 6 */
const PrivateFieldAccessor = (() => {
'use strict';
return class PrivateFieldAccessor {
constructor(initialValue) {
this._privateData = new WeakMap();
this._initialValue = initialValue;
}
init(object, initialValue = this._initialValue) {
this._initialValue = (typeof initialValue === "function") ? initialValue() : initialValue;
this.set(object, this._initialValue);
return this._initialValue;
}
get(object) {
const p = this._privateData.get(object);
return p ? p.value : this.init(object);
}
set(object, value) {
this._privateData.set(object, { value: this._initialValue });
}
};
})();