Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active September 2, 2020 19:54
Show Gist options
  • Save wentout/30b4ba3de79259696a536cdc8325b3d3 to your computer and use it in GitHub Desktop.
Save wentout/30b4ba3de79259696a536cdc8325b3d3 to your computer and use it in GitHub Desktop.
Primitive Vector Magic Proxy Getter )
'use strict';
const vectorObj = new Number(5);
const proxyAsNumber = new Proxy(vectorObj, {
get (target, prop) {
if (prop === Symbol.toPrimitive) {
return function (...args) {
// this -- proxy itself
// args === ['default']
// console.log('THIS', this === vectorObj, args);
return vectorObj.valueOf();
}
}
return target.valueOf();
}
});
console.log(proxyAsNumber);
console.log(vectorObj);
console.log(proxyAsNumber === vectorObj);
try {
console.log(0 + proxyAsNumber); // 5
console.log(2 + vectorObj); // 7
} catch (error) {
console.error(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment