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
console.log(y === undefined); // "true" | |
var y = 3; | |
// will return a value of undefined | |
var myvar = "my value"; | |
(function() { | |
console.log(myvar); // undefined | |
var myvar = "local value"; | |
})(); |
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
// x is defined in the body of the if, but can be referenced outside | |
if (true) { | |
var x = 5; | |
} | |
console.log(x); | |
(function () { | |
var x = 10; | |
console.log(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
foo(1, 2, 3); // Works fine because we declared function using | |
// the function literal at the bottom | |
bar(1, 2, 3); // => ReferenceError: bar is not defined | |
// The functions name is optional | |
// Here we are storing an anonymous function in a variable bar | |
var bar = function (a, b, c) { | |
// ... | |
}; |
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
foo(4, 5); | |
function foo (a, b) { | |
var x = a + b; // a = 4, b = 5, x = 9 | |
function bar (c) { // c = 9 | |
return a + b + c + x; // returns 18 | |
} | |
return (14 * bar(a + b) / x) || 42; | |
} |
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
// this refers to the containing object's context | |
var obj = { | |
value: 0, | |
increment: function (inc) { | |
inc = inc || 1; | |
this.value += inc; | |
} | |
}; | |
obj.increment(); // obj.value = 1 | |
obj.increment(2); // obj.value = 3 |
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
// this refers to the global object (a mistake in the language design) | |
obj.quadruple = function () { | |
var that = this; // common idiomatic workaround for preserving context | |
var helper1 = function () { | |
that.value = that.value * 2; | |
}; | |
var helper2 = function () { | |
this.value = this.value * 2; | |
}; | |
helper1(); |
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
// this refers to the newly created object returned by new | |
var Counter = function (init) { | |
this.value = init || 0; | |
} | |
Counter.prototype.increment = function (inc) { | |
inc = inc || 1; | |
this.value += inc; | |
} | |
var anotherObj = new Counter(); | |
var bad = Counter(); // Don't do this, value gets attached to the global object |
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 incrementer = function (inc) { | |
inc = inc || 1; | |
this.value += inc; | |
}; | |
var foo = { value: 5 }; | |
incrementer.apply(foo, [1]); // foo.value = 6 | |
incrementer.call(foo, 2); // foo.value = 8 |
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 sumParams () { | |
var actualArrayOfArgs = Array.prototype.slice.call(arguments), | |
sum = 0; | |
actualArrayOfArgs.forEach(function (arg) { | |
sum += arg; | |
}); | |
return sum; | |
} | |
sumParams(1, 2, 3); // 6 |
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 add = function (a, b) { | |
if (typeof a !== 'number' || typeof b !== 'number') { | |
throw { | |
name: 'TypeError', | |
message: 'add needs numbers' | |
} | |
} | |
return a + b; | |
} | |
try { |