Greasy Fork

Greasy Fork is available in English.

PrivateProperty

Weakly references a value to a specified object. May be used to create private fields.

当前为 2019-10-27 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/391608/744461/PrivateProperty.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         PrivateProperty
// @namespace    hoehleg.userscripts.private
// @version      0.1
// @description  Weakly references a value to a specified object. May be used to create private fields.
// @author       Gerrit Höhle
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */
const PrivateProperty = (() => {

    const getOrCreate = (privateProperty, keyObj, { initialValue = privateProperty._initialValue, value } = {}) => {
        let data = privateProperty._weakReferences.get(keyObj);
        if (!data) {
            data = { initialValue: initialValue, value: value };
            privateProperty._weakReferences.set(keyObj, data);
        }
        return data;
    };

    return class PrivateProperty {

        constructor(initialValue) {
            this._weakReferences = new WeakMap();
            this._initialValue = initialValue;
        }

        init(object, initialValueSpecific = this._initialValue) {
            const value = (typeof initialValueSpecific === "function") ? initialValueSpecific() : initialValueSpecific;
            this._weakReferences.delete(object);
            this._weakReferences.set(object, { initialValue: initialValueSpecific, value: value });
            return this;
        }

        has(object) {
            return this._weakReferences.has(object);
        }

        get(object) {
            const data = this._weakReferences.get(object);
            if (data) {
                return data.value;
            }
        }

        set(object, value) {
            const data = getOrCreate(this, object);
            data.value = value;
        }

        getInitialValue(object) {
            const data = this._weakReferences.get(object);
            return data ? data.initialValue : this._initialValue;
        }

        getOrCompute(object, fnc) {
            let data = this._weakReferences.get(object);
            if (!data) {
                data = { initialValue: this._initialValue, value: fnc() };
                this._weakReferences.set(object, data);
            }
            return data.value;
        }

        for(object) {
            return {
                init: PrivateProperty.prototype.init.bind(this, object),
                hasValue: PrivateProperty.prototype.hasValue.bind(this, object),
                get: PrivateProperty.prototype.get.bind(this, object),
                set: PrivateProperty.prototype.set.bind(this, object),
                getInitialValue: PrivateProperty.prototype.getInitialValue.bind(this, object),
                getOrCompute: PrivateProperty.prototype.getOrCompute.bind(this, object),
                valueOf: PrivateProperty.prototype.get.bind(this, object),
                toString: () => (String(PrivateProperty.prototype.get.bind(this, object)))
            };
        }
    };
})();