Last active
September 2, 2020 19:54
-
-
Save wentout/30b4ba3de79259696a536cdc8325b3d3 to your computer and use it in GitHub Desktop.
Primitive Vector Magic Proxy Getter )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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