Skip to content

Instantly share code, notes, and snippets.

View knd's full-sized avatar

Khanh Dao knd

  • UC Berkeley
  • Saigon, Vietnam
View GitHub Profile
@knd
knd / Test.java
Created October 3, 2012 03:33
Testing gist
public class Test {
public static void main() {
System.out.println( "hello world" );
}
}
A Fairy Song
Over hill, over dale,
Thorough bush, thorough brier,
Over park, over pale,
Thorough flood, thorough fire!
I do wander everywhere,
Swifter than the moon's sphere;
And I serve the Fairy Queen,
To dew her orbs upon the green;
@knd
knd / scope.js
Last active December 10, 2015 18:08
Some code to demonstrate global and local scope in JavaScript.
ramsay = "chef"; // global variable 'ramsay'
function hellKitchen() {
knd = "chef"; // global variable 'knd'
}
function hellKitchen101() {
var chuckNorris = "chef"; // local variable 'chuckNorris' to this function
// watch out! this function 'assistant()' is locally scoped too
@knd
knd / hoistedVariable.js
Last active December 10, 2015 18:09
Some code to demonstrate hoisted variable in JavaScript.
function hellKitchen() {
if (true) {
var knd = "chef"; // will I be chef?
}
console.log(knd); // yes, I'm a chef due to variable hoisting
// and 'knd' is accessible anywhere in 'hellKitchen'
}
// equivalent snippet of code
@knd
knd / closuredVariable.js
Last active December 10, 2015 18:08
Some code to demonstrate closured variable in JavaScript.
function hellKitchen() {
var knd = "chef";
function hellKitchen101() {
console.log(knd);
knd = "assistant";
}
hellKitchen101();
console.log(knd); // I'm now an 'assistant'
@knd
knd / functionInvoked.js
Last active December 11, 2015 01:28
function invocation
function cook() {
console.log(this); // 'this' is 'Window'
// the default global object of Chrome
function prepare() {
console.log(this); // 'this' is also 'Window'
}
prepare();
}
var cook101 = function() {
@knd
knd / methodInvocation.js
Last active December 11, 2015 01:38
method invocation.
hellKitchen = {
cook: function() {
console.log(this);
}
}
hellKitchen.cook(); // the context 'this' is 'hellKitchen' Object
@knd
knd / callInvocation.js
Last active December 11, 2015 01:38
call() invocation.
hellKitchen = {
cook: function() {
console.log(this);
}
}
myKitchen = {
knd: "amateur chef"
@knd
knd / constructorInvoked.js
Created January 13, 2013 15:48
constructor invocation.
function HellKitchen() {
console.log(this);
this.cook = function() {
console.log(this);
};
}
k = new HellKitchen();
k.cook();
@knd
knd / lastContextQuest.js
Last active December 11, 2015 01:39
final quest for context and function invocation.
hellKitchen = {
cook: function(cb) {
cb();
}
}
myKitchen = {
knd: function() {
console.log(this);
}