Aashish's blog

Notes from javascript the good parts

• javascript, good, and parts

var empty_object = {};
var stooge = {
    "first-name": "Jerome",
    "last-name": "Howard"
};
var middle = stooge["middle-name"] || "(none)";
var status = flight.status || "unknown";
var x = stooge;
x.nickname = 'Curly';
var nick = stooge.nickname;
    // nick is 'Curly' because x and stooge
    // are references to the same object
var a = {}, b = {}, c = {};
    // a, b, and c each refer to a
    // different empty object
a = b = c = {};
    // a, b, and c all refer to
    // the same empty object
// Create a variable called add and store a function
// in it that adds two numbers.
var add = function (a, b) { return a + b;};
// Source: http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/
// If you don't declare your local variables with the var keyword, they are part of the global scope​
var name = "Michael Jackson";

function showCelebrityName () {
	console.log (name);
}

function showOrdinaryPersonName () {	
	name = "Johnny Evers";
	console.log (name);
}
showCelebrityName (); // Michael Jackson​

// name is not a local variable, it simply changes the global name variable​
showOrdinaryPersonName (); // Johnny Evers​

// The global variable is now Johnny Evers, not the celebrity name anymore​
showCelebrityName (); // Johnny Evers​

// The solution is to declare your local variable with the var keyword​
function showOrdinaryPersonName () {	
	var name = "Johnny Evers"; // Now name is always a local variable and it will not overwrite the global variable​
	console.log (name);
} 
for (var i = 1; i <= 10; i++) {
	console.log (i); // outputs 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;​
};

// The variable i is a global variable and it is accessible in the following function with the last value it was assigned above ​
function aNumber () {
console.log(i);
}

// The variable i in the aNumber function below is the global variable i that was changed in the for loop above. Its last value was 11, set just before the for loop exited:​
aNumber ();  // 11​
function showName () {
console.log ("First Name: " + name);
var name = "Ford";
console.log ("Last Name: " + name);
}

showName (); 
// First Name: undefined​
// Last Name: Ford​

// The reason undefined prints first is because the local variable name was hoisted to the top of the function​
// Which means it is this local variable that get calls the first time.​
// This is how the code is actually processed by the JavaScript engine:​

function showName () {
	var name; // name is hoisted (note that is undefined at this point, since the assignment happens below)​
console.log ("First Name: " + name); // First Name: undefined​

name = "Ford"; // name is assigned a value​

// now name is Ford​
console.log ("Last Name: " + name); // Last Name: Ford​
}
// Both the variable and the function are named myName​
var myName;
function myName () {
console.log ("Rich");
}

// The function declaration overrides the variable name​
console.log(typeof myName); // function
 // But in this example, the variable assignment overrides the function declaration​
var myName = "Richard"; // This is the variable assignment (initialization) that overrides the function declaration.​

function myName () {
console.log ("Rich");
}

console.log(typeof myName); // string 
var myName = function () {
console.log ("Rich");
}