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:

  • Confusion About Uniformity In Coding One of the accommodations in JavaScript is that it will naturally constrain any esteem being referenced in a boolean setting to a boolean esteem. Yet, there are situations where this can be as confounding as it is advantage… Read More
  • JavaScript Coding Basic Errors 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… Read More
  • Tips How to Improve Accuracy of Data Entry. Wellsprings of Data Inaccuracies:  Organizations ought to distinguish the sources (both inward and outside) of information incorrectness to plug the hole. Information mistake may come about because of mistaken estee… Read More
  • Regular Mistake Creating memory spills Memory spills are practically unavoidable JavaScript issues in case you're not intentionally coding to keep away from them. There are various routes for them to happen, so we'll simply feature two or three their more typica… Read More
  • Coding Mistake About Thinking there is piece level extension As talked about in our JavaScript Hiring Guide, a typical wellspring of disarray among JavaScript designers (and along these lines a typical wellspring of bugs) is accepting that JavaScript makes another extension for each … Read More

0 comments:

Translate

GoogleTech786. Powered by Blogger.

Subscribe Youtube

Our Facebook Page

Wikipedia

Search results

Popular Posts

Adsense