Skip to content

Instantly share code, notes, and snippets.

@dugdaniels
Created February 22, 2014 04:17
Show Gist options
  • Save dugdaniels/9148672 to your computer and use it in GitHub Desktop.
Save dugdaniels/9148672 to your computer and use it in GitHub Desktop.
function named(){
(function(){
this.foo = function() {
alert('bar');
}
})();
}
named.foo();
@iamdustan
Copy link

function named(){
  (function(){
    this.foo = function() {
      alert('bar');
    }
  })();
}
// calling named causes the interior IIFE to be invoked, but `this` is window in this context.
named()

// at this point `foo` has been exported to window and can be called
foo()

@iamdustan
Copy link

// calling `named()` with this will return an object which has the `foo` function as a member
function named(){
  this.foo = function() {
    alert('bar');
  }
  return this;
}

named().foo()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment