This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var global = (function(g) { | |
return g; | |
})(this); | |
global.a = 'Hello World'; | |
console.log(a); // 'Hello World' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(global){ | |
var x = 2, | |
y = Math.pow(x, 2); | |
global.z = y; | |
})(this); | |
console.log(x); // undefined | |
console.log(y); // undefined | |
console.log(z); // 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(global){ | |
var who = 'World'; | |
var rename = function(s) { | |
who = s; | |
}; | |
var welcome = function(){ | |
return 'Hello ' + who; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var welcome = function(name) { | |
name = name || 'World'; | |
return 'Hello ' + name; | |
}; | |
console.log(welcome()); // 'Hello World' | |
console.log(welcome('foo')); // 'Hello foo' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery | |
var $myFormButton = $('.myForm input[type=button]'); // Potentially slow query | |
$myFormButton.val('Submit'); | |
// lots of code later | |
$myFormButton.val('Processing'); // Didn't need to lookup the element(s) again | |
$myFormButton.attr('disabled', true); // Didn't need to lookup the element(s) again | |
// DOM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery | |
var $myForm = $('#myForm'); | |
// Find child elements in the form without searching the entire DOM | |
$myForm.children('input[type=text]').focus(); | |
$myForm.children('input[type=button]').val('Submit'); | |
// DOM | |
var $myForm = document.querySelector('#myForm'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert a set of DOM elements to an array | |
var domElements = document.querySelectorAll('li'); | |
console.log(domElements.pop()); // Undefined | |
var arrayOfDomElements = Array.prototype.slice.apply(domElements); | |
console.log(arrayOfDomElements.pop()); // [object HTMLLIElement] | |
// Convert function arguments to an array | |
var myFunction = function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = { | |
welcome: 'Hello', | |
name: 'World' | |
}, | |
value; | |
for (var prop in obj) { | |
// Check if the object has that property so that prototypes aren't included. | |
if(obj.hasOwnProperty(prop)) { | |
value = obj[prop]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myArray = [ | |
'Hello', | |
'World', | |
'foo', | |
'bar' | |
]; | |
// Only check the `length` property once by setting `l` at the same time as `i`. | |
for (var i = 0, l = myArray.length; i < l; i++) { | |
console.log(myArray[i]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myArray = [ | |
'Hello', | |
'World', | |
'foo', | |
'bar' | |
]; | |
// Only check the `length` property once by setting `l` at the same time as `i`. | |
for (var i = 0, l = myArray.length; i < l; i++) { | |
console.log(i); |
OlderNewer