Monday, 2 October 2017


How about we characterize a basic protest, and make and occasion of it, as takes after:

var MyObject = function() {}

MyObject.prototype.whoAmI = function() {
    console.log(this === window ? "window" : "MyObj");
};
 
var obj = new MyObject();Presently, for accommodation, we should make a reference to the whoAmI technique, probably so we can get to it simply by whoAmI() instead of the more obj.whoAmI():

var whoAmI = obj.whoAmI;
What's more, just to make certain everything looks copacetic, how about we print out the estimation of our new whoAmI variable:

console.log(whoAmI);

Yields/Output:

function () {
    console.log(this === window ? "window" : "MyObj");
}

Alright, cool. Looks fine.

In any case, now, take a gander at the distinction when we summon obj.whoAmI() versus our accommodation reference whoAmI():

obj.whoAmI();  // outputs "MyObj" (as expected)
whoAmI();      // outputs "window" (uh-oh!)

What turned out badly?

The headfake here is that, when we did the task var whoAmI = obj.whoAmI;, the new factor whoAmI was being characterized in the worldwide namespace. Therefore, its estimation of this is window, not the obj occurrence of MyObject!

Accordingly, in the event that we truly need to make a reference to a current technique for a protest, we should make certain to do it inside that question's namespace, to protect the estimation of this. One method for doing this would be, for instance, as takes after:

var MyObject = function() {}

MyObject.prototype.whoAmI = function() {
    console.log(this === window ? "window" : "MyObj");
};

var obj = new MyObject();
obj.w = obj.whoAmI;   // still in the obj namespace

obj.whoAmI();  // outputs "MyObj" (as expected)
obj.w();       // outputs "MyObj" (as expected)

Related Posts:

  • Begin your own particular site Stamp zuckerberg facebookInterested in producing easy revenue? You require a site. It's THE best approach to profit while you rest. Wix.com also a good way for making your own site. Its so easy way to make website. Beginni… Read More
  • Online market exchanging The generally difficult to-soften universe of contributing up securities exchanges and monetary forms has been aired out wide. Today there is no should be a big whig or store the yachts of Wolf of Wall Street style stock … Read More
  • Get cashback when shopping This isn't just an approach to profit yet in addition to spare cash as an understudy. On the off chance that you take a gander at it distinctively then you are profiting with each buy you would have made in any case, re… Read More
  • Paid for looking through the web  Inspired by procuring money for doing what you as of now do on the web? This must be one of the most straightforward techniques for profiting on the web without truly any exertion or change in your conduct. This cre… Read More
  • No-hazard coordinated wagering free wagers from coordinated bettingHands down the snappiest approach to profit (well, without overstepping the law). Loads of understudies have really made £100s from this method. It's totally legitimate, hazard free, ta… Read More

0 comments:

Translate

GoogleTech786. Powered by Blogger.

Subscribe Youtube

Our Facebook Page

Wikipedia

Search results

Popular Posts

Adsense