by: {
"Name": Engin CAN,
"KnownAs": engincancan,
"Email": engincancan@gmail.com,
"Github": github.com/engincancan
"Twitter": engincan_can
"Date": 21.01.2016,
"License": CC BY-NC-SA
}@istanbulcoders
JavaScript was not developed at Sun Microsystems, the home of Java. JavaScript was developed at Netscape. It was originally called LiveScript, but that name wasn't confusing enough.
Douglas Crockford
First Look of Javascript
Statements - Expressions
var a = b + 2;Operators
Operator
+
-
*
/
%
++
--
Description
Addition
Subtraction
Multiplication
Division
Modulus
Increment
Decrement
Arithmetic Operators
Operators
Operator
==
===
!=
!==
>
<
>=
<=
?
Description
equal to
equal value and equal type
not equal
not equal value or not equal type
greater than
less than
greater than or equal to
less than or equal to
ternary operator
Logical Operators
Value Types
Six data types that are primitives:
and Object
Variables
var amount = 99.99;
amount = amount * 2;
console.log( amount ); // 199.98
// convert `amount` to a string, and
// add "TL" at the end
amount = String( amount ) + " TL";
console.log( amount ); // "199.98 TL"Blocks
var amount = 99.99;
// a general block
{
amount = amount * 2;
console.log( amount ); // 199.98
}
var amount = 99.99;
// is amount big enough?
if (amount > 10) { // <-- block attached to `if`
amount = amount * 2;
console.log( amount ); // 199.98
}Loops
var i = 0;
// a `while..true` loop would run forever, right?
while (true) {
// stop the loop?
if ((i <= 9) === false) {
break;
}
console.log( i );
i = i + 1;
}
// 0 1 2 3 4 5 6 7 8 9for (var i = 0; i <= 9; i = i + 1) {
console.log( i );
}
// 0 1 2 3 4 5 6 7 8 9Functions
function printAmount(amt) {
console.log( amt.toFixed( 2 ) );
}
function formatAmount() {
return amount.toFixed( 2 ) + " TL";
}
var amount = 99.99;
printAmount( amount * 2 ); // "199.98"
amount = formatAmount();
console.log( amount ); // "99.99 TL"Reserved Words
break case class catch const
continue debugger default delete do
else export extends finally for
function if import in instanceof
new return super switch this
throw try typeof var void
while with yieldReserved Words in Strict Mode
implements package protected static let interface
private publicAutomatic Semicolon Insertion
How parser looks at it
(function(w) {
function my(options) {
var myArr = []
console.log('test')
(myArr || []).forEach(function(i) {})
return
{
attr: 'something'
}
}
})(window)
(function(w) {
function my(options) {
var myArr = [];// (1) <- inserted
// (2) Not inserted, lines got merged
console.log('test')(myArr || []).forEach(function(i) {});
return;// (3) <- inserted, breaks the return statement
{
attr: 'something'
}; // (4) <- inserted
}
})(window)
Part 2
One Step Forward
Scopes
function outer() {
var a = 1;
function inner() {
var b = 2;
// we can access both `a` and `b` here
console.log( a + b ); // 3
}
inner();
// we can only access `a` here
console.log( a ); // 1
}
outer();Hoisting
var a = 2;
foo(); // works because `foo()`
// declaration is "hoisted"
function foo() {
a = 3;
console.log( a ); // 3
var a; // declaration is "hoisted"
// to the top of `foo()`
}
console.log( a ); // 2IIFE
(function foo(i) {
if (i === 3) {
return;
}
else {
foo(++i);
}
}(0));Strict Mode
function foo() {
"use strict"; // turn on strict mode
a = 1; // `var` missing, ReferenceError
}
foo();
function foo() {
arguments.callee; // do something with this function object
arguments.callee.caller; // and the calling function object
}
function bigLoop() {
for(var i = 0; i < 100000; i++) {
foo(); // Would normally be inlined...
}
}ES5 Note: undefined in ECMAScript 5 is no longer writable in strict mode, but its name can still be shadowed by for example a function with the name undefined.
Part 3
One Step Backward
Closure
function makeAdder(x) {
// parameter `x` is an inner variable
// inner function `add()` uses `x`, so
// it has a "closure" over it
function add(y) {
return y + x;
};
return add;
}
var plusOne = makeAdder( 1 );
var plusTen = makeAdder( 10 );
plusOne( 3 ); // 4 <-- 1 + 3
plusOne( 41 ); // 42 <-- 1 + 41
plusTen( 13 ); // 23 <-- 10 + 13Prototypal Inheritance
function myMethod() {
return this.value;
}
var object1 = {
get: myMethod,
value: 42
};
var object2 = {
get: myMethod,
value: 3.14159
};
object1.get(); // 42
object2.get(); // 3.14159
myMethod(); // undefinedPrototypal Inheritance
//Why myMethod returns undefined ?
function myMethod() {
return this.value;
}
myMethod(); // undefined//If I set an value on window object
window.value = 123;
myMethod(); // 123
//Alter the method with Stric Mode
function myMethod() {
"use strict";
return this.value; //Uncaught TypeError
}Prototypal Inheritance
var parent = {
get: function() {
return this.value;
},
value: 42
};
var child = Object.create(parent);
child.value = 3.14159;
var grandChild = Object.create(child);
parent.get(); //42
child.get(); //3.14159
grandChild.get(); //3.14159Prototypal Inheritance
var answer = {
get: function() {
return this.value;
},
value: 42
};
var answer1 = Object.create(answer);
answer1.get = function() {
return this.value + "!!";
}
answer1.value = 3.14159;
answer1.get(); // 3.14159!!var answer = {
get: function() {
return this.value;
},
value: 42
};
var answer1 = Object.create(answer);
answer1.get = function() {
return answer.get.call(this) + "!!";
}
answer1.value = 3.14159;
answer1.get(); // 3.14159!!Classical Inheritance
function Answer(value) {
this.value = value;
}
Answer.prototype.get = function() {
return this.value;
}
function answer1(value) {
Answer.call(this, value);
}
answer1.prototype = Object.create(Answer.prototype);
answer1.prototype.constructor = answer1;
answer1.prototype.get = function(){
return Answer.prototype.get.call(this) + "!!";
};Proto vs Classic
function Answer(value) {
this.value = value;
}
Answer.prototype.get = function() {
return this.value;
}
var answer1 = new Answer(3.14159);
answer1.get();
var Answer = {
get: function() {
return this.value;
},
constructor: function(){
this.value = value;
}
};
var answer1 = Object.create(Answer);
answer1.constructor(3.14159);
answer1.get(); // 3.14159!!
Part 4
Others
Transpilers - Supersets
Transpilers - Supersets
Automation Tools
Scaffolding Tools
Dev Tools
console.log(object [, object, …]);
console.log('This is a string', { foo: 'bar' }, { bar: 'foo' });Dev Tools
console.log('%cThis text is styled!',
'color: #86CC00; background-color: blue; font-size: 20px; padding: 3px;');Dev Tools
console.assert(expression, object)
var count = 5;
console.assert(count > 10, 'count is not larger than 10');Dev Tools
console.count(label)
function clickHandler() {
console.count('Click handler called');
}Dev Tools
console.group(object[, object, …]) & console.groupEnd()
console.group("Getting Data");
console.log("Show Loader");
console.log("Get Data Success");
console.log("Hide Loader");
console.groupEnd();Dev Tools
console.table(data)
var data = [
{first_name: 'John', last_name: 'Doe'},
{first_name: 'Jane', last_name: 'Doe'}
];
console.table(data);Dev Tools
console.time(label) & console.timeEnd(label)
console.time('Calculate a');
var a = b + 2;
console.timeEnd('Calculate a');Dev Tools
console.trace()
function foo() {
console.trace();
}
foo();References