Git Product home page Git Product logo

js-deep-proxy's Introduction

JavaScript Deep Proxy

一、简介

对JavaScript原生的Proxy的扩展,增加递归Proxy的功能。

何为Deep Proxy

JavaScript原生的Proxy只能代理传入的对象的直接属性,但对于属性的属性则代理不到,比如:

{
    "a": {
        "b": "xxx"
    }
}

对于上面的对象,使用原生的Proxy能够代理到a属性,但是代理不到a属性上的b属性,这个库就是解决这个问题的,能够·从a一直代理到任意层级的属性。

二、Example

const {deepProxy} = require("./main");

(() => {

    const fuck = {
        "name": "CC11001100",
        "skill": [
            {
                "name": "eat",
                "level": 100,
            },
            {
                "name": "drunk",
                "level": 100,
            },
            {
                "name": "sleep",
                "level": 99,
            },
            {
                "name": "coding",
                "level": 3,
            }
        ],
        "other": {
            "judge": "又菜又爱玩"
        }
    };

    const fucked = deepProxy(fuck, {
        get(target, p, receiver) {
            console.log(`尝试获取属性: ${p}`);
            return target[p];
        }
    });

    console.log(fucked.skill[0].name);

    // Output:
    // 尝试获取属性: skill
    // 尝试获取属性: 0
    // 尝试获取属性: name
    // eat

})();

js-deep-proxy's People

Contributors

cc11001100 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.