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
// String concatenation | |
var stringBuilder = ''; | |
for (var i = 0, len = 30; i < len; i++) { | |
stringBuilder += 'word' + i; | |
} | |
console.log(stringBuilder); |
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
// Antipattern: | |
var x = 10; | |
var y = function () { | |
console.log(x); // logs undefined | |
var x = 20; | |
return x + 10; | |
}; |
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 = ['apple', 'banana', 'cheese', 'donut', 'egg', 'fish']; | |
var send = function (item) { | |
$.ajax({ | |
type: 'post', | |
dataType: 'json', | |
url: '/Foods/Consume', | |
data: { food: item } | |
}); | |
}; |
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
// Antipattern: | |
var preload = function (path) { | |
var img = new Image(); | |
img.src = path; | |
}; | |
preload('apple.png'); | |
preload('banana.png'); | |
preload('cheese.png'); |
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 Car = function(make, model, color) { | |
this.make = make; | |
this.model = model; | |
this.color = color; | |
}; | |
// This way works: | |
var pinto = new Car('ford', 'pinto', 'green'); | |
// but this doesn't: | |
var pinto = Car('ford', 'pinto', 'green'); |
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 factoryAdder = function(initialValue) { | |
// this function "closes over" initialValue, | |
// effectively sealing it into the context | |
return function(numberToAdd) { | |
return initialValue + numberToAdd; | |
}; | |
}; | |
var add5 = factoryAdder(5); | |
console.log(add5(7)); // logs 12 |
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 Namespace = {}; | |
Namespace.module1 = { | |
method1: function() { | |
// code here | |
} | |
}; | |
Namespace.module2 = { | |
method2: 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 App = (function() { | |
var module1 = { | |
method1: function() { | |
// code here | |
} | |
}; | |
var module2 = { | |
ajax: $.ajax |
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
DO WHAT THE **** YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Eli Perelman <http://eliperelman.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE **** YOU WANT TO PUBLIC LICENSE |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>jQuery Deferreds and Promises</title> | |
</head> | |
<body> | |
<div class="container"> | |
<form method="get" action="http://search.twitter.com/search.json"> | |
<input type="search" class="search"> | |
<input type="submit" value="Search teh Tweets!" class="submit"> |