Javascript Puzzle - One Insert

Can you solve this problem without error? The answer is single-line. This is from a dead vacancy, the answer was requested in the answer. I thought this was a smart way to weed out respondents, but I can't answer it without errors.

The obvious solution:

f.moo(alert(f.foo));

But it throws TypeError: callback is undefined { message="callback is undefined", more...}

var f = (function(){
  return {
    foo : "bar",
    moo : function(callback){
      callback.call(this)
    }
  }
})();
//alert "bar" by foo
+3
source share
2 answers

You must pass a f.moofunction. You call alertand pass the result alert(it's nothing).

f.moo(function() { alert(this.foo); });
+10
source

Umm ...

alert(f.foo);

Or did you forget to indicate some requirement?

+2
source

Source: https://habr.com/ru/post/1774367/


All Articles