Created
June 5, 2020 12:34
-
-
Save wentout/c83396ae9153a5f2b5bc37163e111521 to your computer and use it in GitHub Desktop.
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
// just 4 fun & lools | |
class StringBoolean extends String { | |
static get [Symbol.species] () { | |
return Boolean; | |
} | |
get [Symbol.toStringTag] () { | |
return 'StringBoolean'; | |
} | |
get [Symbol.toString] () { | |
return 'StringBoolean'; | |
} | |
get [Symbol.toPrimitive] () { | |
return function (hint) { | |
return this.valueOf(); | |
} | |
} | |
constructor(value) { | |
super(value); | |
const {valueOf} = this; | |
Reflect.defineProperty(this, valueOf, { | |
get () { | |
return valueOf.call(this); | |
} | |
}); | |
} | |
get valueOf () { | |
const value = this.toString(); | |
return function () { | |
return this === 'true' || !(this === 'false'); | |
}.bind(value); | |
} | |
} | |
console.log('\n'); | |
debugger; | |
const t = new StringBoolean('true'); | |
debugger; | |
console.log(t); | |
console.log(+t); | |
console.log(-t); | |
console.log(`${t}`); | |
console.log(t.valueOf()); | |
console.log(t.toString()); | |
console.log('\n'); | |
debugger; | |
const f = new StringBoolean('false'); | |
debugger; | |
console.log(f); | |
console.log(+f); | |
console.log(-f); | |
console.log(`${f}`); | |
console.log(f.valueOf()); | |
console.log(f.toString()); | |
debugger; | |
console.log('\n'); | |
console.log('joint t:', Object.prototype.toString.call(t)); | |
console.log('joint f:', Object.prototype.toString.call(f)); | |
console.log('\n'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment