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-26 提交的版本,查看 最新版本

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴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 */
class PrivateProperty {

    constructor(initialValue) {
        this._privateData = new WeakMap();
        this._initialValue = initialValue;
    }
    
    init(object) {
        this._privateData.set(object, (typeof _initialValue === "function") ? _initialValue() : _initialValue);
        return this;
    }
    
    hasValue(object) {
        return this._privateData.has(object);
    }

    get(object) {
        return this._privateData.get(object);
    }

    set(object, value) {
        this._privateData.set(object, value);
    }

    for(object) {
        return {
            init: PrivateProperty.prototype.init.bind(this, boundObject),
            hasValue: PrivateProperty.prototype.hasValue.bind(this, boundObject),
            get: PrivateProperty.prototype.get.bind(this, boundObject),
            set: PrivateProperty.prototype.set.bind(this, boundObject)
        };
    }
}