POSSIBLE CEE UID WS
2015.11.10.
Setup memory space for Variables / functions
Moving declarations to the top of a SCOPE
You are able to use functions / variables before it has been declared
Hoisting ~ elevate, raise, take up
var poco;
poco = 1;
eco = 1;
var eco;
function displayVariables() {
console.log(pocoeco);
var pocoeco = 'much example';
}
var poco;
poco = 1;
eco = 1;
var eco;
function displayVariables() {
console.log(pocoeco);
var pocoeco = 'much example';
}
var poco;
var eco;
poco = 1;
eco = 1;
function displayVariables() {
var pocoeco;
console.log(pocoeco);
pocoeco = 'much example';
}
function movePoco() {
if (false) {
var x = 1;
}
return;
var y = 1;
}
movePoco();
function movePoco() {
if (false) {
var x = 1;
}
return;
var y = 1;
}
movePoco();
function movePoco() {
var x;
var y;
if (false) {
x = 1;
}
return;
y = 1;
}
movePoco();
function playWithFunctions() {
dog();
cat();
carp();
var dog = function () {
console.log('ufuf');
};
function cat() {
console.log('miauu');
}
var carp = function fish() {
console.log('I am THE carp');
}
}
function playWithFunctions() {
var dog = function () {
};
function cat() {
}
var carp = function fish() {
}
}
function playWithFunctions() {
var dog;
function cat() {}
var carp
//dog, carp undefined!
dog = function () {
};
carp = function fish() {
}
}
Implicit coercion VS Explicit coercion
var a = "42";
var b = a * 2; //Implicit (BAD)
var c = Number(a) * 2; //Explicit (GOOD)
c = parseInt(a, 10) * 2;
c = parseFloat(a, 10) * 2;
NaN
var a = "42f";
var b = a * 2;
NaN
var a = "42f";
var b = a * 2;
b - NaN!!! (Number("42f") - NaN)
parseInt("42f") === 42;
NaN === NaN
isNaN(a)
Associativity, Operator precedence
Which operator function get called first
(higher wins)
What order operator functions get called:
Left-to-right or right-to-left
(same precedence)
var a = 1, b = 2; c = 3;
a = b = c;
console.log(a);
console.log(b);
console.log(c);
console.log(1 < 2 < 3)
console.log(3 < 2 < 1)
console.log(3 < 2 < 1)
3 < 2 < 1
false < 1
Number(false) < 1
console.log(3 < 2 < 1)
3 < 2 < 1
false < 1
Number(false) < 1
// Number(false) === 0
0 < 1
true!!!
Number(true);
Number(false);
Number(null);
Number(undefined);
var a;
if(a !== null && a !== undefined && a !== false) {
// Do something with a
}
var a;
if(a !== null && a !== undefined && a !== false && a !== 0) {
// Do something with a
}
Boolean(null);
Boolean(undefined);
Boolean(false);
Boolean(0)
if(a) {
// Do something with a
}
var a;
a = 0;
if(a || a === 0) {
console.log('Do something with a');
}
var a;
a = 0;
if(false || true) {
console.log('Do something with a');
}
//BY value
var a = 42;
var b;
b = a;
a = 88;
//By reference
var poco = { saysomething: 'ola'}
var eco;
poco = eco;
//By reference function parameter also!
var poco = { saysomething: 'ola'}
var eco;
function doThis(arg) {
arg.saysomething = ketal;
}
eco = poco;
doThis(poco);
console.log(poco);
console.log(eco);
function functionDeclaration() {
};
var functionExpressionVar = function() {
};
var functionExpressionVar = function ihaveaname() {
};
ihaveaname();
var functionExpressionVar = function ihaveaname() {
typeof ihaveaname; //function - i have this in my scope
};
ihaveaname(); //ERROR! there is no ihaveaname in this scope!
if (true) {
function foo() {
return 1;
}
} else {
function foo() {
return 2;
}
}
console.log('foo:' + foo());
if (true) {
function foo() {
return 1;
}
} else {
function foo() {
return 2;
}
}
console.log('foo:' + foo()); // chrome - 2, firefox - 1 :)
var foo; //GOOD!
if (true) {
foo = function foo() {
return 1;
}
} else {
foo = function foo() {
return 2;
}
}
console.log('foo:' + foo());
function makeAdder(x) {
return function(y) {
return x + y;
};
};
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
add5, add10 closure! stores same body def, but different environments
call(), apply(), bind()
"this"
execution stack / function invocations
context / variable environments,
scope chain
prototypal inheritance
....