简体   繁体   English

Javascript ES6代理

[英]Javascript ES6 Proxy

I need to create an object that stores another objects. 我需要创建一个存储其他对象的对象。 Each property of the big object has two properties 'value' and 'callback'. 大对象的每个属性都有两个属性“值”和“回调”。

let bigObj = {
    first: {
        value: true,
        callback: () => {}
    },
    second: {
        value: false,
        callback: () => {}
    }, {...}
}

I want to be able to get and change the value property by using bigObj.first / bigObj.first = "false", and the callback.. through the classic method: bigObj.first.callback = () => {}. 我希望能够通过使用bigObj.first / bigObj.first =“ false”,并通过经典方法回调来获取和更改value属性:bigObj.first.callback =()=> {}。

Each time the property 'value' is changed, I want to call its callback function. 每次更改属性“值”时,我都想调用其回调函数。

Here's what I did 这就是我所做的

var proxy = new Proxy({
    first: {
        value: true,
        callback: () => {}
    }
}, {
    get(target, key) {
        return key in target ? target[key].value : null;
    },
    set(target, key, value) {
        target[key] ? target[key].value = value : target[key] = {value, callback: () => {}};
        key !== 'callback' && target[key].callback();
        return true;
    }
});

The problem is that I can not change the callback property. 问题是我无法更改回调属性。

proxy.first.callback = () => console.log('new cb'); // won't do anything.

Do you have any ideas on how I could change the code so it would work? 您对我如何更改代码有任何想法吗?

Thank you. 谢谢。

The way you have it set up, proxy.first is returning a boolean. 您的设置方式是proxy.first返回一个布尔值。 So then proxy.first.callback = ends up being false.callback = or true.callback = . 因此, proxy.first.callback =最终为false.callback =true.callback = These at least don't throw exceptions, but they're useless. 这些至少不会引发异常,但是它们没有用。 If the value was an object instead of a boolean, you could make the value itself be a proxy, but you can't create a proxy with a non-object as the target. 如果值是对象而不是布尔值,则可以使值本身成为代理,但不能创建以非对象为目标的代理。

Another option would be to have a special value with which you set first, that tells it to insert the callback. 另一个选择是先设置一个特殊值,然后告诉它插入回调。 Below is an example, where if you pass in an object like {callback: () => {}}, then it will insert that as the callback. 下面是一个示例,如果您传入{callback:()=> {}}之类的对象,则会将其插入为回调。 But anything else it will get set as the value. 但是其他任何东西都将被设置为值。

 var proxy = new Proxy({ first: { value: true, callback: () => {} } }, { get(target, key) { return key in target ? target[key].value : null; }, set(target, key, value) { if (value && value.callback) { target[key] ? target[key].callback = value.callback : target[key] = {value: null, callback: value.callback}; return true; } else { target[key] ? target[key].value = value : target[key] = {value, callback: () => {}}; target[key].callback(); return true; } } }); proxy.first = {callback: () => console.log('got a callback')}; proxy.first = false; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM