JSFiddle - React, Tailwind, and code Playground
by Gerald Gillespie
JavaScript
this._foo = 'global';
const myObject = {
_foo: 'bar',
fn: () => {
console.log('fn');
return this._foo;
},
get foo() {
return this._foo;
},
bindFn(arg1, arg2, arg3) {
console.log(arg2, arg3);
return this._foo;
}
}
class SomeClass {
constructor() {
const that = this;
this.sub = new Proxy(myObject, {
get(t, p, r) {
console.log(Object.keys(r));
const mightBeFunction = Reflect.get(t, p);
console.log(typeof mightBeFunction);
return typeof mightBeFunction === 'function' ? Reflect.get(t, p, r).bind(that, null, 'forced') : Reflect.get(t, p, r);
}
});
}
get _foo() {
return 'baz';
}
bindFn(arg1) {
console.log({
arg1
});
return 'someClass.bindFn'
}
}
const mySomeClass = new SomeClass();
console.log(mySomeClass.sub.bindFn('huh'));
// console.log( mySomeClass.sub.foo);
//console.log( mySomeClass.sub.fn());
//console.log(myObject.fn());
const newObject = Object.assign(myObject, {
newFoo() {
return this._foo;
}
});
//console.log( myObject.newFoo());
console.log(myObject.newFoo())