Monday, 2 October 2017


As JavaScript coding procedures and configuration designs have turned out to be progressively modern throughout the years, there's been a relating increment in the multiplication of self-referencing degrees inside callbacks and terminations, which are a genuinely basic wellspring of "this/that perplexity".

Consider this illustration code piece:

Game.prototype.restart = function () {
  this.clearLocalStorage();
  this.timer = setTimeout(function() {
    this.clearBoard();    // what is "this"?
  }, 0);
};
Executing the above code brings about the accompanying mistake:

Uncaught TypeError: undefined is not a function
Why?

It's about setting. The reason you get the above blunder is on the grounds that, when you summon setTimeout(), you are really conjuring window.setTimeout(). Subsequently, the mysterious capacity being passed to setTimeout() is being characterized with regards to the window protest, which has no clearBoard() technique.

A customary, old-program agreeable arrangement is to just spare your reference to this in a variable that would then be able to be acquired by the conclusion; e.g.:

Game.prototype.restart = function () {
  this.clearLocalStorage();
  var self = this;   // save reference to 'this', while it's still this!
  this.timer = setTimeout(function(){
    self.clearBoard();    // oh OK, I do know who 'self' is!
  }, 0);
};

Then again, in more up to date programs, you can utilize the predicament() strategy to go in the best possible reference:

Game.prototype.restart = function () {
  this.clearLocalStorage();
  this.timer = setTimeout(this.reset.bind(this), 0);  // bind to 'this'
};

Game.prototype.reset = function(){
    this.clearBoard();    // ahhh, back in the context of the right 'this'!
};

Related Posts:

  • HTML - Colors Hues are vital to give a decent look and feel to your site. You can determine hues on page level utilizing <body> tag or you can set hues for singular labels utilizing bgcolor characteristic. The <body> tag has … Read More
  • HTML - Embed Multimedia Some of the time you have to include music or video into your site page. The least demanding approach to add video or sound to your site is to incorporate the extraordinary HTML tag called <embed>. This label makes the… Read More
  • HTML - Overview HTML remains for Hypertext Markup Language, and it is the most generally utilized dialect to compose Web Pages. Hypertext alludes to the path in which Web pages (HTML archives) are connected together. Along these lines, t… Read More
  • HTML - Fonts Textual styles assume an imperative part in making a site more easy to understand and expanding content coherence. Textual style face and shading depends totally on the PC and program that is being utilized to see your page … Read More
  • HTML - Forms HTML Forms are required, when you need to gather a few information from the site guest. For instance, amid client enlistment you might want to gather data, for example, name, email address, charge card, and so on. A shape w… Read More

0 comments:

Translate

GoogleTech786. Powered by Blogger.

Subscribe Youtube

Our Facebook Page

Wikipedia

Search results

Popular Posts

Adsense