JS元编程meta programming

元编程定义:

The ablity to treat program as data.

The ablity to to read, generate, analyze or transform other programs, and even modify itself while running.

1.Macro(sweet.js)

可以用宏扩展JS语言

2.Reflection

happens during runtime

Reflection有三种

1.Introspection

inspect itself during runtime

Object.keys

2.self-modification

change functionality during runtime

3.Intercession

intercept itself

 

为何有用:

1.Expressivity to model our problems

2.elegant and developer friendly interfaces

 

提供抽象来隐藏实现细节

new Proxy(target , handler)

拦截和自定义行为对象的基础操作(assignment, enumeration, function invocation)

Babel unsupported feature

Due to the limitations of ES5 ,Proxies cannot be transpiled or  polyfilled.

See support in various Javascript engines.

one of  the few features that essentially requires engine support.

 

Unlike most of ES6 ,which is syntax sugar.

new Proxy(target , handler)

target可以是{} , [] , function ,或者另一个Proxy。

 

 

Reflect

perform all these operations on objects

overload基本操作符 . = ()

 

Proxy

Instrumentation

observable objects

function observale(obj , onChange){   
    return new Proxy(obj, {
        set(target , key , value){
            Reflect.set(target, key ,value);
            onChange({key , value});
        }
    };
}

 

Deeply traversing a JSON "graph"

Another tool in your toolbox to build your DREAM INTERFACES.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值