How to avoid method inside class from getting overriden outside the class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to avoid method inside class from getting overriden outside the class?

I just found this hack for google crome Dinojump, where we override the gameOver function of the game instance and set it to empty function to avoid the gameOver function from getting implemented. Is there a way of avoiding it? https://code.sololearn.com/WV9716plmNaT/?ref=app

21st Nov 2021, 7:12 PM
CodeSmith
CodeSmith - avatar
3 Answers
+ 2
You can use Object.freeze (for the whole object) or Object.defineProperty (set writable and configurable to false) https://stackoverflow.com/questions/41639501/prevent-overriding-javascript-function
21st Nov 2021, 9:04 PM
ChaoticDawg
ChaoticDawg - avatar
0
Or you can define your method as a Symbol. https://code.sololearn.com/WvLArjA8FaP5/?ref=app
22nd Nov 2021, 5:22 PM
Artur
Artur - avatar
0
But of course, doing so doesn't prevent from defining new method. So in order to prevent defining new method you should follow above mentioned steps. With Symbol method you just can make sure that you'll never override it with another property.
22nd Nov 2021, 6:42 PM
Artur
Artur - avatar