What is the purpose of self.self == Self-checking in JavaScript?

backbone.js starts with:

//Establish the root object, `window` (`self`) in the browser, or `global` on the server. //We use `self` instead of `window` for `WebWorker` support. var root = (typeof self == 'object' && self.self == self && self) || (typeof global == 'object' && global.global == global && global); 

What is self.self == self for? When can it be a lie?
Same thing about global.global == global .

+5
source share
2 answers
 function Mistake(x); self = this; x.on("event", function() { console.log(self); }); } new Mistake(…); 

Did you notice this? Now we have a global self , which is not self , that the reference expects. Therefore, it checks whether self is really a global object, which is likely to happen when self is an object, and the object has a "global variable" self as a property that points to the object itself.

The same goes for global .

+1
source

I think you can find at least part of the explanation here.

0
source

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


All Articles