1

I've noticed that in the Google text adventure game easter egg they have a function call everytime you enter in a variable on the javascript console.

How is this done?

  • Possible duplicate of [How do I get console input in javascript?](https://stackoverflow.com/questions/3120761/how-do-i-get-console-input-in-javascript) – Baruch Oct 03 '18 at 19:28
  • @Baruch uh, no, that one is about CLI input in the SpiderMonkey engine. – Bergi Oct 03 '18 at 20:32

1 Answers1

3

It doesn't work for arbitrary variables. It doesn't work for arbitrary expressions. It only works for those commands that are given (yes, no, north, south, east, west, up, down, grab, why, inventory, use, help, exits, map, and friends).

It doesn't really work for variables anyway. What you get when you enter yes is the global window.yes property - and they have defined a getter for it. This getter will be evaluated, return the string "yes", and asynchronously log the next output to the console.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    `Object.defineProperty(window, "yes", { get() { setTimeout(console.log, 0, "hello"); return "yes"; })`. You can check the code of the easter egg in your debugger, search for `defineProperty` – Bergi Oct 06 '18 at 15:48
  • 1
    You missed a `}` at the end, it would be `Object.defineProperty(window, "yes", { get() { setTimeout(console.log, 0, "hello"); return "yes"; }})`. But thanks for the code! – Levi Stepanek Oct 06 '18 at 15:51