26
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Scope & Functions in ECMAScript 6.0

Embed Size (px)

Citation preview

Page 1: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Page 2: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

d = 'ev';

var d;

console.log(d);

var d;

d = 'ev';

console.log(d);

Only the declarations themselves are hoisted, while any assignments or

other executable logic are left in place.

Page 3: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

test(); // 1

var test;

function test() {

console.log(1);

}

test = function () {

console.log(2);

};

function test() {

console.log(1);

}

test(); // 1

test = function () {

console.log(2);

};

Page 4: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

function add(num1, num2) {

var sum = num1 + num2;

return sum;

}

Add

[[Scope]]

Scope Chain

0

Global Object

this Windows

windows (object)

document (object)

add (function)

… …

add.length === 2;

Object.getPrototypeOf(add) === Function.prototype;

Page 5: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Activation object

this Windows

arguments [ 5 , 10 ]

num1 5

num2 10

sum undefined

var Total = add( 5 , 10 );

add(5,10)

Execution

context

Scope

chain

Scope Chain

0

1

Global Object

this Windows

windows (object)

document (object)

add (function)

… …

Page 6: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var color = "blue";

function changeColor() {

var anotherColor = "red";

function swapColors(){var tempColor = anotherColor;anotherColor = color;color = tempColor;// color, anotherColor, and tempColor// are all accessible here.

}// color and anotherColor are accessible here, // but not tempColor.swapColors();

}//only color is accessible herechangeColor();

Windows

color

changeColor()

anotherColor

swapColos() tempColor()

Page 7: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

function addEvent() {

var id = "xdi9592";

document.getElementById("save-btn").onclick = function (event) {

saveDocument( id );};

}From parent

scope

Page 8: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Scope Chain

0

1

Activation object

this Windows

arguments []

id “xdi9592”

addEvent()

Execution context

Scope chain

Scope Chain

0

1

Global Object

this Windows

windows (object)

document (object)

addEvent (function)

saveDoc (function)

Closure

[[Scope]]

Page 9: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Scope Chain

0

1

2

Activation object

this Windows

arguments []

id “xdi9592”

Global Object

this Windows

windows (object)

document (object)

addEvent (function)

saveDoc (function)

Closure

execution context

[[Scope]]

Activation object

(closure)

this Windows

arguments []

event (object)

Page 10: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var funcs = [];

for ( var i = 0; i < 10; i ++) {

funcs.push( function() { console.log(i); });

}

funcs.forEach( function(func) {

func(); // outputs the number "10" ten times

});

Page 11: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var funcs = [];

for ( var i = 0; i < 10; i ++) {

funcs.push(( function(value) {

return function() {

console.log(value);

}

}(i)));

}

funcs.forEach( function(func) {

func(); // outputs 0, 1, 2, 3, up to 9

});

Page 12: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var funcs = [];

for ( let i = 0; i < 10; i ++) {

funcs.push( function() { console.log(i); });

}

funcs.forEach( function(func) {

func(); // outputs 0, 1, 2, 3, up to 9

});

Page 13: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

const PI = 3.14159;

// Can't re-assignPI = 3; console.log(PI); // 3.14159

// Can't re-initializeconst PI = 4; console.log(PI); // 3.14159

// Can't re-declarevar PI = 4; console.log(PI); // 3.14159

Page 14: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Page 15: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Page 16: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Undefined

Null

function history( lang = "C", year = 1971 ) {

// lang = lang || "C";

// year = year || 1971;

return lang + " was created around the year " + year;

}

Page 17: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

// defining rest parameters with 3 dot syntaxfunction push(array, ...items) {

items.forEach(function(item) {array.push(item);console.log( item );

});}

// 1 fixed + 3 variable parametersvar planets = [];push(planets, "Mercury", "Venus", "Earth");

Page 18: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Spread

operator

let values = [25, 50, 75, 100];

Math.max.apply( Math , values );

Math.max(...values);

Math.max(...values , 200 , 300 );

Page 19: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Page 20: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var f= x => x;

var f= (n1,n2) => n1+n2;

var f= id => ({id:id,name:"T"});

var f = function(x) {return x;

}

var f = function(n1,n2) {return n1 + n2;

}

var f = function(id) {

return {id: id,name: "T"

};}

Page 21: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var PageHandler = {id: "123456",

init: function() {document.addEventListener("click", function(event) {

this.doSomething(event.type); // error}, false);

},

doSomething: function(type) {console.log("Handling " + type + " for " + this.id);

}};

Global

Page 22: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var PageHandler = {id: "123456",

init: function() {document.addEventListener("click", (function(event) {

this.doSomething(event.type);}).bind(this), false);

},

doSomething: function(type) {console.log("Handling " + type + " for " + this.id);

}}

Page 23: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var PageHandler = {id: "123456",

init: function() {document.addEventListener("click",

event => this.doSomething(event.type), false);},

doSomething: function(type) {console.log("Handling "+type+" for " + this.id);

}};

Page 24: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

let v = ( function(name) {return {

getName() {return name;

}};

}( "Eyal" ) );

let v = ( (name) => {return {

getName() {return name;

}};

})( "Eyal" );

Page 25: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

http://www.2ality.com/

Understanding ECMAScript 6

http://ecmascript6.org/

A Few New Things Coming To JavaScript

HARMONY OF DREAMS COME TRUE

Harmony specification_drafts

Page 26: Scope & Functions in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

eyalvardi.wordpress.com