Reflect and Proxy

Method[] methods = MyObject.class.getMethods();

for(Method method : methods){
    System.out.println("method = " + method.getName());
}
Constructor[] constructors = aClass.getConstructors();
Function.prototype.apply.call(func, obj, args)

func.apply(obj, args)

Reflect.apply.call(func, obj, args)
var obj = new F(...args)

var obj = Reflect.construct(F, args)
class Greeting {
   constructor(name) { this.name = name }
   greet() { return `Hello ${this.name}` }
}

const greetingFactory = name => Reflect.construct(Greeting, [name]);

const obj = greetingFactory('Tuturu');
obj.greet();

+ Proxy

const $ = document.querySelector.bind(document);
Element.prototype.on = Element.prototype.addEventListener;
console.log( $('some').innerHTML );
error TypeError: Cannot read property 'innerHTML' of null
const $ = selector =>
  new Proxy(
    document.querySelector(selector)||Element,
    { get: (target, key) => Reflect.get(target, key) }
   )
;
console.log( $('some').innerHTML ); // undefined
const emptyObj = () =>
 new Proxy({},
   {
     get: (target, key, receiver) => (
           Reflect.has(target, key) ||
           Reflect.set(target, key, emptyObj()),
           Reflect.get(target, key, receiver)
     )
   }
 )
;
const path = emptyObj();

path.to.virtual.node.in.empty.object = 123;

console.log(path.to.virtual.node.in.empty.object); // 123
const emptyObj = () =>
 new Proxy({}, {
     get: (target, key, receiver) => (
        key == 'toJSON' ? () => target : (
              Reflect.has(target, key) ||
              Reflect.set(target, key, emptyObj()),
              Reflect.get(target, key, receiver)
            )
     )
   }
 );
const path = emptyObj();
path.to.virtual.node.in.empty.object = 123;

console.log(JSON.stringify(path));
// {"to":{"virtual":{"node":{"in":{"empty":{"object":123}}}}}}
try {
   Object.defineProperty(obj, name, desc);
   // property defined successfully
} catch (e) {
   // possible failure (and might accidentally catch the wrong exception)
}
/* --- OR --- */
if (Reflect.defineProperty(obj, name, desc)) {
   // success
} else {
   // failure
}
const myObject = Object.create(null);
myObject.foo = 123;

myObject.hasOwnProperty === undefined; // true

Object.prototype.hasOwnProperty.call( myObject, 'foo' );

Reflect.ownKeys(myObject).includes('foo')

TypeScript

emitDecoratorMetadata

Reflect.getMetadata()

Reflect and Proxy

By Vladimir

Reflect and Proxy

  • 99