Why does underscore use `root` instead of` this`?

In some cases, I see aliases to reduce the search chain, but in this case it is a simple single-line alias without reduction.

var root = this;

I think it is thismore descriptive, as it points to windowthe browser or to many different global variables if JavaScript is server-side.

If it was supposed to be aliased, I feel like

var global = this;

will be more descriptive.

Why is the word used root? I heard that root is used in the context of the "root user", but in the context of JavaScript development, I do not understand.

+4
source share
3 answers

this .

function doSomething() {
    function helper() {
        alert(this); // I'm helping!
    }

    alert(this);
    helper();
}
someElement.onclick = doSomething;

, , ( null , ).

var root = this; , -, , .

root... , .

+3

, , .

. root , this . . = JavaScript ?.

, root _.noConflict: function

_.noConflict = function() {
    root._ = previousUnderscore;
    return this;
};

root,

_.noConflict = function() {
    this._ = previousUnderscore;
    return _;
}.bind(this);

aliased, , var global = this; . ?

, global - , global - idenfier, Node . root " " " ".

+1

root noConflict

 _.noConflict = function() {
    root._ = previousUnderscore;
    return this;
  };

here thisrefers to underlining and refers to a global area .

as root instead of global, it's a matter of opinion

0
source

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


All Articles