Skip to content

Instantly share code, notes, and snippets.

View singhkumarhemant's full-sized avatar
🎯
Focusing

Hemant Kumar Singh singhkumarhemant

🎯
Focusing
View GitHub Profile
// ==> ES6 <== //
function MyFunc(greeting) {
this.greeting = greeting;
}
MyFunc.prototype.myTeamArr = function (teamArr) {
return teamArr.forEach((player) => {
console.log(this.greeting + player);
});
};
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 ');
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);
};
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);
});
// ==> 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
function myComponent() {
const button = document.getElementById('myButton');
button.addEventListener('click', () => {
console.log('I AM CLICK');
this.handleClick(); // lexical `this`
});
}
const arr = [1, 2, 3];
const squares = arr.map(x => x * x);
// Traditional function expression
const squares = arr.map(function (x) { return x * x });
//ES5
var updateNameAndDept_1 = function setNameDept(name, dept) {
return { name: name, dept:dept };
};
// ES6
var updateNameAndDept_2 = (name, dept) => ({ name: name, dept:dept });
//ES5
var getDoc_1 = function getDoc() {
console.log(document);
};
//ES6
var getDoc_2 = () => { console.log(document); };
//ES5
function multiply_1(params) {
return params * 2
}
multiply_1(2); // 4
//ES6
var multiply_2 = params => params * 2
multiply_2(2); // 4