Last active
July 21, 2019 06:39
-
-
Save ungarson/e58ed106fc0b2f467dd2eb81f9b3596b to your computer and use it in GitHub Desktop.
Make range object by creating has method in the object's proxy in javascript
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
let range = { | |
start: 0, | |
finish: 10 | |
} | |
range = new Proxy(range, { | |
has(target, property) { | |
if (property >= range.start && range.finish >= property) return true; | |
else return false; | |
} | |
}) | |
export {range}; | |
// Example of usage | |
// console.log(5 in range); // true | |
// console.log(20 in range); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment