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 arr = [], | |
i = arr.length; | |
while (i--) { | |
// Do something with array. | |
} |
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 foo() { | |
console.log('global foo'); | |
} | |
function bar() { | |
console.log('global bar'); | |
} | |
function hoistMe() { | |
console.log(typeof foo); // "function" | |
console.log(typeof bar); // "undefined" |
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
// Calling callback when callback is a method of an object | |
var myapp = {}; | |
myapp.elementHeight = 100; | |
myapp.setElementHeight = function (node) { | |
node.style.height = this.elementHeight; | |
}; | |
// Where setElementHeight is our callback | |
// findNodes() function which call our callback | |
var findNodes = function (callback) { |
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
// Self-defining functions | |
var greetings = function () { | |
console.log("Hello!"); | |
greetings = function () { | |
console.log("Goodbye!"); | |
} |
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
// Clean globals with name-spacing pattern | |
// Anti-pattern | |
// Pollution with 5 objects added to global. | |
function add() {} | |
function subtract() {} | |
var container = {}; | |
var data = {}; | |
data.person = {name: 'John', email: 'smith@gmail.com'}; | |
data.account = {}; |
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
// Implement private members with closure | |
function Person() { | |
var name = 'Frank'; | |
this.getName = function () { // Privileged method, has access to private property. | |
return name; | |
} | |
} | |
var user = new Person(); | |
console.log(user.name); // undefined |
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
// Module pattern | |
APP.utilities.array = (function () { | |
// Private properties | |
var array_string = "[object Array]", | |
ops = Object.prototype.toString(), | |
// Private methods | |
inArray = function (haystack, needle) { | |
for (var i = 0; i < haystack.length; i = i + 1) { |
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
// Module pattern that creates constructor function. | |
APP.utilities.Array = (function (app, global) { | |
// Dependencies | |
var uobj = APP.utilities.object, | |
ulang = APP.utilities.lang, | |
// Private properties | |
Constructor; |
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 Sandbox() { | |
// turning arguments into an array | |
var args = Array.prototype.slice.call(arguments), | |
// last argument is the callback | |
callback = args.pop(), | |
// modules can be passed as an array or as individual parameters | |
modules = (args[0] && typeof args[0] === "string") ? args : args[0], | |
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
// Always use lates version | |
// Not all selectors are equaly fast | |
// Fastest to slowest selectors. | |
$('#selector-by-id'); | |
$('div', 'input'); | |
$('.class-selectors'); | |
// And the slowest are pseudo selectors | |
$(':visible, :hidden, [attribute=value]'); |
OlderNewer