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 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
var value; | |
// Antipattern: | |
if (value === undefined) { | |
// code here | |
} | |
// Good pattern: |
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
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
<!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"> |
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
define(function () { | |
var HttpError = function (msg, code) { | |
var ex = new Error(msg || 'Unknown HTTP error'); | |
ex.code = code; | |
ex.toString = function() { | |
return 'Error: ' | |
+ (this.code ? '' : '(' + this.code + ') ') | |
+ this.message; | |
}; |