Last active
August 16, 2020 12:57
-
-
Save Abrifq/2190f035b81b3ea2641769075163442b to your computer and use it in GitHub Desktop.
This is a test script to test the new "private fields" in classes. (May not work now due to incomplete implementation)
This file contains 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
class PrivateNameTest{ | |
#lucky_number; //This hopefully can be tried without the semicolon once the proposal is implemented. | |
constructor(){ | |
this.#lucky_number = 42; | |
} | |
guessLuckyNumber(guessedNumber){return guessedNumber===this.#lucky_number;} | |
} | |
class PrivateDefTest{ | |
#arePrivateVariablesAwesome = true; | |
constructor(){} | |
amIAwesome(){return this.#arePrivateVariablesAwesome;} | |
} | |
class PrivateMethodTest{ | |
#RNG(max,min){ | |
//if methods doesn't work but variables do, put "=function" in between the "RNG" and "()" | |
return Math.ceil( ( Math.random() * (max - min + 1) ) + (min - 1) ); | |
} | |
#diceSides; | |
constructor(sideCount){ | |
this.#diceSides = sideCount; | |
} | |
roll(){ | |
return this.#RNG(this.#diceSides,1); | |
} | |
} | |
//If those won't give syntax errors, code below will run. | |
console.log((new PrivateNameTest()).guessLuckyNumber(3)); //false | |
console.log((new PrivateDefTest()).amIAwesome()); //true | |
console.log((new PrivateMethodTest(6)).roll()); //some number between 1-6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turns out, you can't extend on private fields.
Let me fix that.