Skip to content

Instantly share code, notes, and snippets.

View yangsu's full-sized avatar

Yang Su yangsu

  • San Francisco, CA
  • X @ys65
View GitHub Profile
@yangsu
yangsu / javascript-hoisting.js
Created January 29, 2013 20:44
JavaScript Hoisting
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";
})();
@yangsu
yangsu / javascript-scope.js
Created January 29, 2013 20:46
JavaScript Scope
// 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
})();
@yangsu
yangsu / javascript-funcs.js
Created January 29, 2013 21:05
JavaScript Funtions Example
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) {
// ...
};
@yangsu
yangsu / javascript-inner-function.js
Last active December 11, 2015 22:09
JavaScript Inner Function
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;
}
@yangsu
yangsu / javascript-func-invoke-method.js
Last active December 11, 2015 22:09
JavaScript Function Invocation - Method Invocation
// 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
@yangsu
yangsu / javascript-func-invoke-func.js
Created January 29, 2013 21:09
JavaScript Function Invocation - Function Invotation
// 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();
@yangsu
yangsu / javascript-func-invoke-constructor.js
Created January 29, 2013 21:09
JavaScript Function Invocation - Constructor
// 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
@yangsu
yangsu / javascript-func-invoke-apply-call.js
Created January 29, 2013 21:10
JavaScript Function Invocation - Apply/Call
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
@yangsu
yangsu / javascript-args.js
Created January 29, 2013 21:20
JavaScript Arguments
function sumParams () {
var actualArrayOfArgs = Array.prototype.slice.call(arguments),
sum = 0;
actualArrayOfArgs.forEach(function (arg) {
sum += arg;
});
return sum;
}
sumParams(1, 2, 3); // 6
@yangsu
yangsu / javascript-exception.js
Last active December 11, 2015 22:09
JavaScript Exception Example
var add = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw {
name: 'TypeError',
message: 'add needs numbers'
}
}
return a + b;
}
try {