How to use javascript proxy for nested objects

前端 未结 4 761
礼貌的吻别
礼貌的吻别 2020-12-02 13:22

I have this code in js bin:

var validator = {
  set (target, key, value) {
    console.log(target);
    console.log(key);
    console.log(value);
    if(isO         


        
4条回答
  •  [愿得一人]
    2020-12-02 13:44

    A slight modification on the example by Michał Perłakowski with the benefit of this approach being that the nested proxy is only created once rather than every time a value is accessed.

    If the property of the proxy being accessed is an object or array, the value of the property is replaced with another proxy. The isProxy property in the getter is used to detect whether the currently accessed object is a proxy or not. You may want to change the name of isProxy to avoid naming collisions with properties of stored objects.

    Note: the nested proxy is defined in the getter rather than the setter so it is only created if the data is actually used somewhere. This may or may not suit your use-case.

    const handler = {
      get(target, key) {
        if (key == 'isProxy')
          return true;
    
        const prop = target[key];
    
        // return if property not found
        if (typeof prop == 'undefined')
          return;
    
        // set value as proxy if object
        if (!prop.isProxy && typeof prop === 'object')
          target[key] = new Proxy(prop, handler);
    
        return target[key];
      },
      set(target, key, value) {
        console.log('Setting', target, `.${key} to equal`, value);
    
        // todo : call callback
    
        target[key] = value;
        return true;
      }
    };
    
    const test = {
      string: "data",
      number: 231321,
      object: {
        string: "data",
        number: 32434
      },
      array: [
        1, 2, 3, 4, 5
      ],
    };
    
    const proxy = new Proxy(test, handler);
    
    console.log(proxy);
    console.log(proxy.string); // "data"
    
    proxy.string = "Hello";
    
    console.log(proxy.string); // "Hello"
    
    console.log(proxy.object); // { "string": "data", "number": 32434 }
    
    proxy.object.string = "World";
    
    console.log(proxy.object.string); // "World"

提交回复
热议问题