var me = this 的值是多少; [英] What is the value of var me = this;

查看:22
本文介绍了var me = this 的值是多少;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ExtJS 源代码中发现了这种模式.

I find this pattern all over the ExtJS source code.

method: function() {
  var me = this;
  ...
  me.someOtherMethod();
}

他们为什么不直接使用this?always 经常定义 me 有什么好处(除了不必输入 2 个字符)?我可以理解他们是否试图通过闭包来维护上下文,但这是在根本没有闭包的地方完成的.

Why don't they just use this? Is there some advantage to always often defining me (outside of not having to type 2 characters)? I can understand if they are trying to maintain the context via closure, but it's done in places where there is no closure at all.

来自 Ext.panel.Panel 的示例:

An example from Ext.panel.Panel:

disable: function(silent) {
    var me = this;

    if (me.rendered) {
        me.el.addCls(me.disabledCls);
        me.el.dom.disabled = true;
        me.onDisable();
    }

    me.disabled = true;

    if (silent !== true) {
        me.fireEvent('disable', me);
    }

    return me;
},

推荐答案

一个原因是因为通过缩小,me 可以变得比 this 短得多.

One reason is because with minification, me can become much shorter than this.

这篇关于var me = this 的值是多少;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆