Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #5-2
  2. 프로그램은 나만의 언어를 만들어가는 과정이다.
  3. - 가장 중요한건 함수의 이름!
  4.  
  5. `__proto__`를 통해 Object에 바로 프로토타입 체이닝을 쓸 수 있다.
  6.  
  7. ```javascript
  8. //기존의 체이닝
  9. var ONLY_FOR_CHAIN = {}
  10. parent = function(){
  11. //constructor
  12. if(argunments[] === ONLY_FOR_CHAIN) return;
  13. }
  14. child = function(){}
  15. child.prototype = new parent(ONLY_FOR_CHAIN);//체이닝을 위한 도구
  16.  
  17. ==> 위 문제를 해결
  18.  
  19. parnet = class{;
  20. child = class extends parent{
  21. }
  22.  
  23. -->
  24. child = class extends class{mehtod(){}} extends class{mehtod(){}}{ //class chain이 가능하다;;
  25. }
  26.  
  27. ==> ES6에서 개선된 proto
  28. a.__proto__= {
  29. method(){},
  30. method(){}
  31. }
  32. -->
  33. a.__proto__= {
  34. __proto__(){}
  35. }
  36.  
  37. //ex)
  38.  
  39. a = {
  40. method(){return 3;}
  41. }
  42. b = {
  43. method(){return 4;}
  44. }
  45.  
  46. test = {__proto__:a);
  47. test.method(); //3
  48. test.__proto__ = b;
  49. test.method(); //4
  50.  
  51. /*
  52. 아래 코드는 에러가 발생.
  53. Test = class{}
  54. test = new Test();
  55. test.__proto__ = b;
  56. */
  57.  
  58.  
  59. ========AOP=========
  60.  
  61. A = function(){}
  62. A.prototype = {
  63. methodA(){},
  64. methodB(){}
  65. };
  66.  
  67. map = new WeakMap();
  68.  
  69. A.prototype = new Proxy(A.prototype, {
  70. get(target, key){
  71. if(!key in target)) return function(){console.log('not method');};
  72. if(!map.has(target)){
  73. map.set(target, {});
  74. }
  75. if(!map.get(target)[key]){
  76. map.get(target)[key] = function(...arg){
  77. console.log(key, arg);
  78. return target[key](...arg);
  79. }
  80. }
  81. }
  82. });
  83.  
  84. a = new A)_;
  85. a.methodA(1,2,3,4); //methodA, 1,2,3,4
  86. a.xxx(); //not method
  87.  
  88. target instanceof class
  89. target.__proto__ === class.prototype
  90. target.__proto__ === class.prototype.__proto__
  91.  
  92. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement