Chapter 12

Here are the solutions to the Exercises, for the Chapter 12, Deep Dive into Metaprogramming chapter.

Exercise 1

 'use strict'​;
 
 class​ Counter {
 constructor​() {
 this​.count = 0;
  }
 
  incrementBy(value) { ​this​.count += value; ​return​ ​this​.count; }
  decrementBy(value) { ​this​.count -= value; ​return​ ​this​.count; }
 }
 
 const​ call = ​function​(counter, method, ...data) {
 const​ methodToCall = Reflect.​get​(counter, method);
 return​ Reflect.apply(methodToCall, counter, data);
 };
 
 const​ counter = ​new​ Counter();
 
 console.log(call(counter, ​'incrementBy'​, 10)); ​//10
 console.log(call(counter, ​'decrementBy'​, 7)); ​//3
 console.log(counter.count); ​//3

Exercise 2

  • target refers to sample
  • propertyName refers to "foo"
  • receiver refers to proxy

Exercise 3

There’s a small twist in this exercise. At first thought, merely Proxy.revocable(instance, {}); appears to be sufficient. However, that approach will result in an incompatible type error when proxy.getYear() is called. The type of the proxy is not the same as the type of Date. To fix this we have to use the bind() method, like so:

 'use strict'​;
 
 const​ createRevocableProxy = ​function​(instance) {
 const​ handler = {
  get: ​function​(target, propertyName ​/*, unusedReceiver */​) {
 return​ Reflect.​get​(target, propertyName).bind(target);
  }
  };
 
 const​ { proxy, revoke } = Proxy.revocable(instance, handler);
 
  setTimeout(revoke, 3000);
 
 return​ proxy;
 };
 
 const​ proxy = createRevocableProxy(​new​ Date());
 
 const​ callGetYear = ​function​() {
 try​ {
  console.log(1900 + proxy.getYear());
  } ​catch​(ex) {
  console.log(ex.message);
  }
 };
 
 callGetYear(); ​//current year like 2018
 
 setTimeout(callGetYear, 1000); ​//current year like 2018
 
 setTimeout(callGetYear, 5000);
 //Cannot perform 'get' on a proxy that has been revoked

Exercise 4

 'use strict'​;
 
 const​ createPlayMethodProxy = ​function​(instance) {
 const​ handler = {
  get: ​function​(target, propertyName) {
 if​(propertyName.startsWith(​'play'​)) {
 const​ activity = propertyName.substring(4);
 
 if​(target.activities.includes(activity)) {
 return​ () => ​`I love to play ​${activity}​`​;
  } ​else​ {
 return​ () => ​`I don't play ​${activity}​`​;
  }
  } ​else​ {
 throw​ ​new​ Error(​`​${propertyName}​ is not a function`​);
  }
  }
  };
 
 return​ ​new​ Proxy(instance, handler);
 };
 
 const​ proxy = createPlayMethodProxy({ activities: [​'Football'​, ​'Tennis'​] });
 
 console.log(proxy.playTennis()); ​//I love to play Tennis
 console.log(proxy.playFootball()); ​//I love to play Football
 console.log(proxy.playPolitics()); ​//I don't play Politics
 
 try​ {
  console.log(proxy.dance());
 } ​catch​(ex) {
  console.log(ex.message); ​//dance is not a function
 }

Exercise 5

 'use strict'​;
 
 const​ proxy = ​new​ Proxy(Set.prototype, {
  get: ​function​(target, propertyName, receiver) {
 return​ receiver.has(propertyName);
  }
 });
 
 Reflect.setPrototypeOf(Set.prototype, proxy);
 
 const​ fruits = ​new​ Set([​'Apple'​, ​'Banana'​, ​'Orange'​, ​'Jack Fruit'​]);
 
 console.log(fruits.size); ​//4
 console.log(fruits.Apple); ​//true
 console.log(fruits.Banana); ​//true
 console.log(fruits.Orange); ​//true
 console.log(fruits[​'Jack Fruit'​]); ​//true
 console.log(fruits.Tomato); ​//false
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.237.58