JavaScript“我”=“这个”,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13364503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:33:45  来源:igfitidea点击:

JavaScript "me" = "this", why?

javascriptextjs

提问by James Allardice

I saw in many source codes:

我在很多源代码中看到:

var me = this;

specially in Ext-JS 4 (JS framework). Why doing such thing? Is there any other reason or you just want for a variable to be called like "me" instead of "this"?

特别是在 Ext-JS 4(JS 框架)中。为什么要做这样的事情?是否有任何其他原因,或者您只想将变量称为“我”而不是“这个”?

Thank you.

谢谢你。

回答by James Allardice

Usually so you can keep a reference to thisinside a scope in which thisrefers to something else (like a callback function, for example).

通常,您可以this在一个范围内保留对其他内容的引用this(例如回调函数)。

Consider this example, in which the click event handler function has a different context to what you may expect (thisdoesn't refer to an instance of MyClass):

考虑这个例子,其中 click 事件处理函数具有与您预期不同的上下文(this不引用 的实例MyClass):

var MyClass = function (elem) {
    this.elem = elem;
    this.name = "James";
    elem.addEventListener("click", function () {
        alert(this.name); //oops
    }, false);
};

Now consider this example, in which we store a reference to the value of thisinside the constructor function, and use that inside the callback function:

现在考虑这个例子,我们this在构造函数内部存储了一个对 的值的引用,并在回调函数中使用它:

var MyClass = function (elem) {
    var me = this;
    this.elem = elem;
    this.name = "James";
    elem.addEventListener("click", function () {
        alert(me.name); //works!
    }, false);
};

The callback function can refer to a variable that was declared in the outer function, even after that function has returned (the MyClassconstructor returns as soon as it's executed the addEventListener). This is a demonstration of a closure.

回调函数可以引用在外部函数中声明的变量,即使在该函数返回之后(MyClass构造函数在执行后立即返回addEventListener)。这是一个闭包的演示。

回答by matt

Though of course closures are the more obvious reason for doing this, I just wanted to add that another reason can be to reduce the size of the minified version of a javascript file.

虽然当然闭包是这样做的更明显的原因,但我只想补充一点,另一个原因可能是减少 javascript 文件的缩小版本的大小。

thisas a keyword cannot be renamed in the process of minifying the file, while a local variable can. In other words, whenever you would use this (4 characters), instead a 1 character local variable can be used.

this作为关键字不能在缩小文件的过程中重命名,而局部变量可以。换句话说,无论何时使用它(4 个字符),都可以使用 1 个字符的局部变量。

Consider the following example functionof ExtJS's Ext.data.Store:

考虑以下ExtJS 的示例函数Ext.data.Store

filterBy: function(fn, scope) {
    var me = this;

    me.snapshot = me.snapshot || me.data.clone();
    me.data = me.queryBy(fn, scope || me);
    me.fireEvent('datachanged', me);
    me.fireEvent('refresh', me);
}

(note there's no closure involved here)

(注意这里不涉及闭包)

and its minified version:

及其缩小版本:

filterBy:function(b,a){var c=this;c.snapshot=c.snapshot||c.data.clone();c.data=c.queryBy(b,a||c);c.fireEvent("datachanged",c);c.fireEvent("refresh",c)}

(151 characters/bytes)

(151 个字符/字节)

Now, let's compare it to the minified version if we did not assign thisto a local variable:

现在,如果我们没有分配this给局部变量,让我们将它与缩小版本进行比较:

filterBy:function(b,a){this.snapshot=this.snapshot||this.data.clone();this.data=this.queryBy(b,a||this);this.fireEvent("datachanged",this);this.fireEvent("refresh",this)}

(170 characters/bytes)

(170 个字符/字节)

As you can see the version with a local variable only takes 88% of the size of the function which uses thiseach time instead.

正如您所看到的,带有局部变量的版本只占用this每次使用的函数大小的 88% 。

Especially in big libraries this can reduce the file size quite a bit.

特别是在大型库中,这可以大大减少文件大小。

回答by Nate

Setting me=thisallows you to use the thisvariable from an outer scope in an inner scope.

设置me=this允许您this在内部作用域中使用外部作用域中的变量。

var Outer= function () {
        var me = this;
        me.x = "outerx";
        me.inner = {
            x: "innerx",
            displayValues: function () {
                console.log(me.x); //outerx
                console.log(this.x); //innerx
            }
        };
    };

    new Outer().inner.displayValues();

回答by Konstantin Dinev

Basically this utilizes closurein javascript. Read this about closure.

基本上这利用了 javascript 中的闭包阅读有关关闭的内容。

It is used to carry the particular instance of thisto function calls where thishas a different meaning.

它用于承载具有不同含义的this函数调用的特定实例this