1

I understand about self invoking anonymous functions like so:

(function () {
    //...
})();

and that most of the time they are used for creating closures and abstracting variables in frameworks. However I've also seen sources in different sites that declare anonymous functions but they do not invoke them. I'm curious as to how those are used. Are they invoked when the script is loaded? Note that in the sources tab they don't look like .js files. Instead they have a name like so:

extensions::utils, or extensions::Event etc.

They look like they're part of some kind of framework/library? I'm not sure. For example the extensions::Event has this code inside:

(function($Object, $Function, privates, cls, superclass) {'use strict';
  function Event() {
    var privateObj = $Object.create(cls.prototype);
    $Function.apply(cls, privateObj, arguments);
    privateObj.wrapper = this;
    privates(this).impl = privateObj;
  };
  if (superclass) {
    Event.prototype = Object.create(superclass.prototype);
  }
  return Event;
}) //<-Self invocation missing.

As you can see this is just declared but not used. The only thing I can think of is that this anonymous function is actually assigned to a variable called Event and is part of the extensions object or something?

PentaKon
  • 4,139
  • 5
  • 43
  • 80
  • 2
    It depends on what the context is. If that were loaded directly into a web browser or Node.js, it would be useless dead code. However, if it's in a file that's part of some sort of source code structure, a build step could very will do something with that code (like invoke it). – Pointy Dec 15 '15 at 21:23
  • 2
    Where **exactly** do you see code like that? – Pointy Dec 15 '15 at 21:25
  • https://purgegamers.true.io/g/ under no-domain – PentaKon Dec 15 '15 at 21:25
  • Under what? I don't see what you're referring to in that link. –  Dec 15 '15 at 21:28
  • 2
    @Konstantine copy the information from the outside resource **into your question**. Links like that aren't helpful long-term because they can go away. – Pointy Dec 15 '15 at 21:28
  • Looks to me like a reusable fragment of code that gets eval'd when data is supplied, a little bit like NodeJS modules. –  Dec 15 '15 at 21:30
  • @squint Yes but how does one have access to it? Can we eval() the body of a .js file? – PentaKon Dec 15 '15 at 21:37
  • @Konstantine - Yes you can. Here's a simple example: if you run the following in your console `eval((function () { console.log('hi'); }))()`, it would print "hi". Notice how the eval is surrounding an unevaluated self-invoking anonymous function and then evaluating the result of the eval with the end (). – boombox Dec 15 '15 at 22:12
  • One can fetch it and then eval it, providing the invoking part that's missing at the bottom. How it's fetched will depend on the environment. Is that the entire content of the file? If so, I'd say that's almost definitely what it's for. –  Dec 15 '15 at 23:27

1 Answers1

0

Since the only content these files have is the non self-invoking function the most probable explanation is that the whole content of the file gets evaled and executed by appending a () at the end. This way we can have access to anonymous functions without assigning them to a variable i.e: var reference = function () {} and still execute them when needed and not during their creation.

EDIT: These 'files' seem to be things used by google chrome. I found them in unrelated websites. Also I was unable to find them when I opened the same site from firefox.

PentaKon
  • 4,139
  • 5
  • 43
  • 80