Skip to content

Instantly share code, notes, and snippets.

@alexweber
Last active April 15, 2016 17:55
Show Gist options
  • Save alexweber/a62eeaceb86029f0b7abf91884e70671 to your computer and use it in GitHub Desktop.
Save alexweber/a62eeaceb86029f0b7abf91884e70671 to your computer and use it in GitHub Desktop.
Hoisting examples
// Example 1
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();
// Example 2
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
@alexweber
Copy link
Author

// Example 1
var foo = 1;
function bar() {
        var foo;
    if (!foo) {
      foo= 10;
    }
    alert(foo);
}
bar();

@tanmancan
Copy link

// Same as this:

var foo = 1,
    bar = function() {
        if (!foo) {
            var foo = 10;
        }
        alert(foo);
    };
bar();

@ethanhinson
Copy link

for(var i = 0; i < myVar.length; i++) {
  (function($) {
    var new = myVar.property;
    // stuff
   //now you can use
   $.append(new)
  })(jQuery);
}

@dalguete
Copy link

This works too

for(var i = 0; i < myVar.length; i++) {
  !function() {
    var new = myVar.property;
    // stuff
  }();
}

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