PrivateProperty

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

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greasyfork.org/scripts/391608/744693/PrivateProperty.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         PrivateProperty
// @namespace    hoehleg.userscripts.private
// @version      0.2
// @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 = (() => {
    let prototypeFunctions;

    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 = this._weakReferences.get(object) || {
                initialValue: this._initialValue
            };
            data.value = value;
            this._weakReferences.set(object, data);
        }

        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) {
            const bound = prototypeFunctions.map(([propName, fnc]) => ({ [propName]: fnc.bind(this, object) }));
            return Object.assign({}, ...bound);
        }
    }

    prototypeFunctions = Object.getOwnPropertyNames(PrivateProperty.prototype)
        .filter(propName => typeof PrivateProperty.prototype[propName] === "function")
        .filter(propName => propName !== "constructor")
        .filter(propName => propName !== "for")
        .map(propName => ([ [propName], PrivateProperty.prototype[propName] ]));

    return PrivateProperty;
})();