to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running. — Wikipedia
its own structure and behavior at runtime • Introspection: read the structure of a program • Self-modification: change program structure • Intercession: change language semantics
{ 'Mr. Robot': 20, 'The Simpsons': 598, 'House of Cards': 52 }; Object.keys(tvShows).forEach(title => { const episodes = tvShows[title]; console.log(`${title} has been on air for ${episodes} episodes.`); });
Boolean, String) • A completely unique, immutable value • Cannot be coerced into a String • Symbol() creates a new Symbol, but don't use the new keyword
[Symbol.iterator]() { let i = -1, length = this.length; return { next() { if (++i < length) return { value: 'Na', done: false }; if (i === length) return { value: 'Batman!', done: false }; return { done: true }; } }; } } let bm = new CapedCrusader(8); Array.from(bm); console.log(...bm); // => Na Na Na Na Na Na Na Na Batman!
a Proxy object • Trap and define custom behavior for fundamental object operations • new Proxy(target, handler); • target - The object to proxy • handler - An object containing the behaviors to redefine (the traps) • Revocable Proxies constructed with Proxy.revocable let { proxy, revoke } = new Proxy.revocable(target, handler);
}; const proxy = new Proxy(obj, { get(target, property) { if (property === 'secret') return console.log('Unauthorized. This incident has been reported'); return target[property]; } }); console.log(obj.secret); // => SUPER SECRET console.log(proxy.secret); // => Unauthorized. This incident has been reported. // => undefined console.log(proxy.foo); // => bar
{ const newArgs = [ 'And I\'m never going to dance again', ...args, 'Guilty feet have got no rhythm' ]; return Reflect.apply(target, thisArg, newArgs); } }; const whisper = new Proxy((...args) => console.log(...args), whisperHandler); whisper('User created successfully'); // => And I'm never going to dance again User created succesfully Guilty feet have got no rhythm