JavaScript对象通用的Getter/Setter?

3

是否可能为所有JavaScript对象定义通用的getter/setter?

下面是我想要实现的伪代码。基本上,person和animal路由getter和setter到CustomGetter和CustomSetter。

function NewPerson()
{
    var person;
    var animal;

    var person.name = 'John Doe';
    console.log("Name: " + person.name); //Prints "Name: JOHNDOE CUSTOM"

    var animal.type = 'Herbivore';
    console.log("Animal: " + animal.type); //Prints "Animal: HERBIVORE CUSTOM"

    console.log("Age: " + person.age); //Prints "Age: NON EXISTANT PROPERTY";
}

function CustomGetter(theObj, propertyName)
{
    if(theObj.hasproperty(propertyName))
        return ToUpperCase(theObj.propertyName);
    else
    {
        return "NON EXISTANT PROPERTY";
    }
}

function CustomSetter(theObj, propertyName, value)
{
    if(theObj.hasproperty(propertyName))
        theObj.propertyName = value + " CUSTOM";
    else
    {
        console.log("NON PROPERTY TO SET");
    }
}

感谢你!

请查看Object.defineProperty: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty - Psi
1
对象没有通用的 setter/getter,你需要在 Proxy 中包装你的对象来获得这个功能:https://dev59.com/5msz5IYBdhLWcg3wcnZb - Patrick Evans
3个回答

2

我最近也做了类似的事情。如果你在原型上使用defineProperty,就可以将其应用于所有实例。就像这样:

   Object.defineProperty(Object.prototype, 'test', {
        get: function () {
            return "a test";
        }
    });

var test = new Array(2);
console.log(test); //a test

现在任何对象都将拥有属性“test”。

2
谢谢。我看了一下,似乎 Proxy 比 defineProperty 更适合。使用 Proxy,我可以捕获甚至不存在的属性。 - Water

0

你应该使用代理陷阱来实现你想要的功能。

文档:MDN - 代理处理程序

你可以很容易地通过类和原型来完成,但我更喜欢使用自定义处理程序中的getter/setter陷阱来实现闭包。

通常,它看起来像这样:

var Person = function(profile={})
{
    var required     = ["age", "name", "profession"];
    var buildProfile = function(p)
    {
        for(var prop of required)
        {
            if(!(prop in p))
                throw new ReferenceError
                ("⛔ Missing required property to profile '" +(prop+" in "+JSON.stringify(profile))+ "'");
        }
        return p;
    }(profile);

    var anotherPrivateFunc = function()
    {
        // ...
    };

    var publicMethods = 
    {
        status: function(prop, value)
        {
            // ...
        },
        isAdult: function()
        {
            return this.age >= 18;
        },
    };

    Object.assign(profile, publicMethods);
    return new Proxy(profile,
    {
        get: function(target, prop, receiver)
        {
            if(prop in target)
            {
                switch(prop)
                {
                    case "name":
                        return target[prop].toUpperCase(); break;
                }
            }
            return Reflect.get.apply(Object.getOwnPropertyDescriptor, arguments);
        }
    });
};

var person = new Person({age:32, name: "Water", profession:"developper"})

person.name      // will return WATER
person.age       // --> 32
person.isAdult() // --> true

var person = new Person({age:32}) // will throw an cusotm error
Uncaught ReferenceError: ⛔ Missing required property to profile 'name in {"age":3}'

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接