Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why passing parameters to anon function when creating scoping

Sorry, the title sucks but I couldn't think of a better one.

The ShadowDOM.js file in Polymer does this:

(function(scope) {
  "use strict";
  var unsafeUnwrap = scope.unsafeUnwrap;
  var wrap = scope.wrap;
  var nonEnumDescriptor = {
    enumerable: false
  };
  function nonEnum(obj, prop) {
    Object.defineProperty(obj, prop, nonEnumDescriptor);
  }
  function NodeList() {
    this.length = 0;
    nonEnum(this, "length");
  }
  NodeList.prototype = {
    item: function(index) {
      return this[index];
    }
  };
  nonEnum(NodeList.prototype, "item");
  function wrapNodeList(list) {
    if (list == null) return list;
    var wrapperList = new NodeList();
    for (var i = 0, length = list.length; i < length; i++) {
      wrapperList[i] = wrap(list[i]);
    }
    wrapperList.length = length;
    return wrapperList;
  }
  function addWrapNodeListMethod(wrapperConstructor, name) {
    wrapperConstructor.prototype[name] = function() {
      return wrapNodeList(unsafeUnwrap(this)[name].apply(unsafeUnwrap(this), arguments));
    };
  }
  scope.wrappers.NodeList = NodeList;
  scope.addWrapNodeListMethod = addWrapNodeListMethod;
  scope.wrapNodeList = wrapNodeList;
})(window.ShadowDOMPolyfill);

Simple question: why passing the parameter window.ShadowDOMPolyfill?

Yes, this is an anonymous function that gets executed straight away. Yes all of the variables will stay within the function, avoid pollution. Yes scope will be the same as window.ShadowDOMPolyfill.

This is a pattern I have seen many times. I totally understand why it's good not to pollute the global scope with variables, etc. But, why passing window.ShadowDOMPolyfill as first parameter? As far as I know, the Window object is totally available within functions... so what's the difference between the code above and:

(function() {
  "use strict";
  var scope = window.ShadowDOMPolyfill;
  ...
})();

...?

like image 833
Merc Avatar asked May 03 '16 08:05

Merc


People also ask

Why would you use an anonymous function?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.

Why do we need anonymous function in JavaScript?

An anonymous function is not accessible after its initial creation, it can only be accessed by a variable it is stored in as a function as a value. 3. This function is useful for all scenarios. An anonymous function can be useful for creating IIFE(Immediately Invoked Function Expression).

What is an anonymous function and when should you use it?

Anonymous functions are often arguments being passed to higher-order functions, or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function.

Why are anonymous functions frequently used with event handlers?

More generally, the point of using anonymous functions is that they do not require a name, because they are "event handlers" bound to a specific event on a specific object. In this case, the object is the entire Document Object Model, and the anonymous function executes when the entire DOM has loaded.


1 Answers

It's considered good practice to define the parameters your function needs in order to do the work it's assigned to do, in the argument list.

While it's perfectly possible to do it the way you propose, it encourages a lying API, in a sense that you can't look at the function signature and understand what goes into the function.

In this particular instance, the two examples are identical in functionality, however, imagine there being more parameters, and their usages and definitions are scattered across the function body.

(function(oneThing, anotherThing, aThirdThing) {
  ...
})(window.oneThing, window.anotherThing, window.aThirdThing);

is more readable than

(function() {
  ... // with your vars somewhere inside.
})();

In your example, you must enforce a convention with your developers to always put these definitions at the top, to maintain readability. However, the language already helps you enforce that with the argument list.

like image 191
Madara's Ghost Avatar answered Sep 23 '22 10:09

Madara's Ghost