元编程定义:
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.