bernie2436
bernie2436

Reputation: 23901

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

I am trying to understand this minified code from quojs. In the code, e (minified variable: not my doing!) is defined on line 3. Then, within the scope of the function that defines the function e, e is defined again. What is going on? Is e getting defined on line 3 and then e.e getting defined in line 6? I am trying to get a feel for the javascript object model : but things getting defined within the scope of their own definition is definitely throwing me!

(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);

Upvotes: 0

Views: 48

Answers (3)

Oriol
Oriol

Reputation: 288100

You have different scopes, with different e in each one.

Simplifying your code,

var e = 'abc';
e; // 'abc'
function foo() {
    var e = 123;
    e; // 123
}
e; // 'abc'

Upvotes: 1

Amadan
Amadan

Reputation: 198314

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

Due to function scope, those are different variables.

Upvotes: 0

Matt
Matt

Reputation: 20786

Is e getting defined on line 3 and then e.e getting defined in line 6?

No, the var e on line 4 is declaring it as local to the function. So there is no conflict between these two es.

Upvotes: 2

Related Questions