Skip to content

Instantly share code, notes, and snippets.

View gskachkov's full-sized avatar

Oleksander Skachkov gskachkov

  • Itera Consulting
  • Ukraine, Kiev
View GitHub Profile
const metaHandler = {
get: function (dt, trapName) {
console.log(dt, trapName);
return Reflect[trapName];
}
};
const dummyTarget = {};
const baseHandler = new Proxy(dummyTarget, metaHandler);
const target = {};
var target = { id : "boo" };
var p = new Proxy(target, {
get: function(target, property, receiver) {
},
set: function(target, property, value, receiver) {
},
defineProperty: function(target, property, propDesc) {
},
deleteProperty: function(target, property) {
},
const proxy = new Proxy(
{ id: "id-0" },
{
set: function (target, property, value, receiver) {
if (property.charAt(0) === "_") return false;
target[property] = value;
return true;
}
...
}
var target = { id: "id-0" };
var handler = {
set: function (target, property, value, receiver) {
if (property.charAt(0) === "_") return false;
target[property] = value;
return true;
}
};
var proxy = new Proxy(target, handler);
const foo = (a, b, c) => `${a}:${b}:${c}`;
const proxy = new Proxy(foo, {
apply: function(target, thisArg, argumentsList) {
return target.apply(thisArg, argumentsList);
}
});
console.log(proxy("1", "2", "3")); // 1:2:3
class A {
constructor (value) { this.value = value; }
getValue() { return this.value; }
}
const proxiedClass = new Proxy(A, {
construct: function(target, [ first ], newTarget) {
return new target(first + ":proxy-value");
}
});
const handler = {
get: function(target, property, receiver) {
if (property.startAt(0) === "_") return undefined;
return target[property];
}
};
const proxy = new Proxy({}, handler);
console.log(proxy.id); // 'ABCD'
const handler = {
defineProperty: function(target, property, descriptor) {
if (property.charAt(0) === "_") return false;
return Reflect.defineProperty(target, property, descriptor);
}
};
const proxy = new Proxy({}, handler);
proxy.id = "ABCD"; // { id: 'ABCD' }
var proxiedObject = new Proxy(target, handler);
var weaver = function (target, methodName) {
var before = function () {
console.log(arguments);
};
var old = target[methodName];
target[methodName] = function () {
before.apply(this, arguments);
return old.apply(this, arguments);
};