javascript - What purpose does this serve? IIFE assignment to var in Snap.svg.js -


my self-directed javascript study has led me reading libraries found following snippet (truncated brevity). i'm using firefox firebug on windows apache server (xampp).

i figured snippet below suffice if needs it, entire library can found here: snap.svg.js on github

var snap = (function(root) {  snap.version = "0.4.0";    function snap(w, h) {           	  // can either width, height or   	if (w) {  		if (w.nodetype) {                        // deterimines if parameter dom element  			return wrap(w);  		}  		if (is(w, "array") && snap.set) {        // deterimines if parameter array  			return snap.set.apply(snap, w);  		}  		if (w instanceof element) {              // deterimines if parameter snap.element  			return w;  		}  		if (h == null) {                         // elimination determines if parameter dom element id.  			w = glob.doc.queryselector(string(w));  			return wrap(w);  		}  	}    <numerous public , private properties , methods>	  .  .  .  glob.win.snap = snap;  return snap;  }(window || this));

firebug shows snap object in window context before instantiating user objects. wondering mechanism injecting snap object dom. that's when noticed "var snap". initially, thought it. but, since didn't break app when changed variable name or deleted it, became confused.

further investigation resulted in discovery @ bottom of iife... specifically, "glob.win.snap = snap". since "window" being passed iife, seems what's creating snap object in window. changing name glob.win.snappy confirmed this.

i'm still learning please correct me if i'm wrong. i'm trying understand what's going on library. seems function snap() being injected window context via glob.win.snap assignment. don't see "var snap" @ top or "return snap" doing anything. in fact, can rem them out , seems function fine. so, first question: 2 lines serve function i'm not seeing?

a secondary question is: "this" fallback parameter refer to? limited understanding of snap is used within window namespace wouldn't "this" window?

just when think i'm beginning make paradigm shift classical prototypical language, run across code , sets me back. i'd appreciate insight.

i had @ referenced source code, here more condensed version:

var snap = (function(root) {     snap.version = "0.4.0";      function snap(w, h) {}      var glob = {         win: root.window,         doc: root.window.document     };      ...      glob.win.snap = snap;     return snap; }(window || this));  snap.plugin(...); 

it seems function snap() being injected window context via glob.win.snap assignment. don't see "var snap" @ top or "return snap" doing anything.

you correct, declaration of var snap = ...; , assignment via return snap; superfluous, since variable lives in global scope (i. e. window object) , declared glob.win.snap = snap;

i assume keept var declaration since pretty standard when using class pattern:

var myclass = (function() {     function myclass(n) {}     return myclass; })(); 

a secondary question is: "this" fallback parameter refer to? limited understanding of snap is used within window namespace wouldn't "this" window?

in javascript environments, root object not called window (e.g. global in node.js). window || this evaluate root object, no matter called.

you see such dependency injections in javascript modules. see https://carldanley.com/js-module-pattern/ more.

however, seems library not run if there no window object available due var glob = { win: root.window, ... } assignment. might have kept this in there because part of standard module pattern.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -