Skip to content Skip to sidebar Skip to footer

How To Call "lexical This" Or Arrow Syntax For This In Javascript?

I want to call myObj.sayHello with myObj as its running scope. I know about bind, and it works fine. var myObj = {'name1':'BP', 'sayHello':function(){ consol

Solution 1:

We don't have the fat arrow syntax in JavaScript, unfortunately. This source:

http://javascriptweblog.wordpress.com/2012/04/09/javascript-fat-city/

Says it's on the way though.

Try this:

setTimeout(function(){
    myObj.sayHello();
}
, 2000);

Solution 2:

I think you probably already know about which browsers do/don't support that syntax.

Given that, the problem in your code is that you're not invoking the sayHello() method.

//                             vv---invoke the functionsetTimeout(() => myObj.sayHello(), 2000);

Otherwise the body of the function merely references .sayHello without invoking it.

Test it in Firefox, and you'll see it work.

Post a Comment for "How To Call "lexical This" Or Arrow Syntax For This In Javascript?"