This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==> ES6 <== // | |
| function MyFunc(greeting) { | |
| this.greeting = greeting; | |
| } | |
| MyFunc.prototype.myTeamArr = function (teamArr) { | |
| return teamArr.forEach((player) => { | |
| console.log(this.greeting + player); | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function MyFunc(greeting) { | |
| this.greeting = greeting; | |
| } | |
| MyFunc.prototype.myTeamArr = function (teamArr) { | |
| return teamArr.forEach(function (player) { | |
| console.log(this.greeting + player); }.bind(this)); // see this | |
| }; | |
| var welcomeTeam = new MyFunc('Welcome '); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var greeting = "Hello"; | |
| function MyFunc(greeting) { | |
| this.greeting = greeting; | |
| } | |
| MyFunc.prototype.myTeamArr = function (teamArr) { | |
| return teamArr.forEach(function (player) { | |
| console.log(this.greeting + player); },this); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var greeting = "Hello"; | |
| function MyFunc(greeting) { | |
| this.greeting = greeting; | |
| } | |
| MyFunc.prototype.myTeamArr = function (teamArr) { | |
| var that = this; // -->(1) | |
| return teamArr.forEach(function (player) { | |
| console.log(that.greeting + player); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==> ES5 <== // | |
| var greeting = "Hello"; | |
| function MyFunc(greeting) { | |
| this.greeting = greeting; | |
| } | |
| MyFunc.prototype.myTeamArr = function (teamArr) { // -->(3) | |
| return teamArr.forEach(function (player) { // -->(2) | |
| // Doesn't work: give undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function myComponent() { | |
| const button = document.getElementById('myButton'); | |
| button.addEventListener('click', () => { | |
| console.log('I AM CLICK'); | |
| this.handleClick(); // lexical `this` | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const arr = [1, 2, 3]; | |
| const squares = arr.map(x => x * x); | |
| // Traditional function expression | |
| const squares = arr.map(function (x) { return x * x }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //ES5 | |
| var updateNameAndDept_1 = function setNameDept(name, dept) { | |
| return { name: name, dept:dept }; | |
| }; | |
| // ES6 | |
| var updateNameAndDept_2 = (name, dept) => ({ name: name, dept:dept }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //ES5 | |
| var getDoc_1 = function getDoc() { | |
| console.log(document); | |
| }; | |
| //ES6 | |
| var getDoc_2 = () => { console.log(document); }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //ES5 | |
| function multiply_1(params) { | |
| return params * 2 | |
| } | |
| multiply_1(2); // 4 | |
| //ES6 | |
| var multiply_2 = params => params * 2 | |
| multiply_2(2); // 4 |