在这段代码中:为什么e被定义在定义e的括号中

In this code: why is e getting defined in the brackets that define e?

本文关键字:定义 段代码 为什么 代码      更新时间:2024-04-05

我正试图从quojs中理解这个缩小的代码。在代码中,e(缩小的变量:不是我在做的!)是在第3行定义的。然后,在定义函数e的函数的范围内,再次定义e。发生了什么事?e在第3行被定义,然后e.e在第6行被定义?我试图了解javascript对象模型:但在它们自己的定义范围内定义的东西肯定会让我大吃一惊!

(function() {
  var e;
  e = function() {
    var e, t, n;
    t = [];
    e = function(t, r) {
      var i;
      if (!t) {
        return n()
      } else if (e.toType(t) === "function") {
        return e(document).ready(t)
      } else {
        i = e.getDOMObject(t, r);
        return n(i, t)
      }
    };
    n = function(e, r) {
      e = e || t;
      e.__proto__ = n.prototype;
      e.selector = r || "";
      return e
    };
    e.extend = function(e) {
      Array.prototype.slice.call(arguments, 1).forEach(function(t) {
        var n, r;
        r = [];
        for (n in t) {
          r.push(e[n] = t[n])
        }
        return r
      });
      return e
    };
    n.prototype = e.fn = {};
    return e
  }();
  window.Quo = e;
  "$$" in window || (window.$$ = e)
}).call(this);

e在第3行被定义,然后e.e在第6行被定义?

不,第4行的var e声明它是函数的本地。因此,这两个e之间没有冲突。

您有不同的作用域,每个作用域中有不同的e

简化您的代码,

var e = 'abc';
e; // 'abc'
function foo() {
    var e = 123;
    e; // 123
}
e; // 'abc'
var e = function() {
  var e = 1;
  console.log(e); // prints a *number*
};
e();
console.log(e); // prints a *function*

由于函数的作用域,这些是不同的变量。