#5-2 프로그램은 나만의 언어를 만들어가는 과정이다.
- 가장 중요한건 함수의 이름!
__proto__
를 통해 Object에 바로 프로토타입 체이닝을 쓸 수 있다.
//기존의 체이닝
var ONLY_FOR_CHAIN = {}
parent = function(){
//constructor
if(argunments[] === ONLY_FOR_CHAIN) return;
}
child = function(){}
child.prototype = new parent(ONLY_FOR_CHAIN);//체이닝을 위한 도구
==> 위 문제를 해결
parnet = class{;
child = class extends parent{
}
-->
child = class extends class{mehtod(){}} extends class{mehtod(){}}{ //class chain이 가능하다;;
}
==> ES6에서 개선된 proto
a.__proto__= {
method(){},
method(){}
}
-->
a.__proto__= {
__proto__(){}
}
//ex)
a = {
method(){return 3;}
}
b = {
method(){return 4;}
}
test = {__proto__:a);
test.method(); //3
test.__proto__ = b;
test.method(); //4
/*
아래 코드는 에러가 발생.
Test = class{}
test = new Test();
test.__proto__ = b;
*/
========AOP=========
A = function(){}
A.prototype = {
methodA(){},
methodB(){}
};
map = new WeakMap();
A.prototype = new Proxy(A.prototype, {
get(target, key){
if(!key in target)) return function(){console.log('not method');};
if(!map.has(target)){
map.set(target, {});
}
if(!map.get(target)[key]){
map.get(target)[key] = function(...arg){
console.log(key, arg);
return target[key](...arg);
}
}
}
});
a = new A)_;
a.methodA(1,2,3,4); //methodA, 1,2,3,4
a.xxx(); //not method
target instanceof class
target.__proto__ === class.prototype
target.__proto__ === class.prototype.__proto__