View functionDefined.js
This file contains 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 user = { | |
name: "Ahmar", | |
keys: [1, 2, 3], | |
printName: function() { | |
this.keys.forEach(function(x) { | |
console.log(this.name); | |
}, this) | |
} | |
} |
View arrowUndefined.js
This file contains 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 user = { | |
name: "Ahmar", | |
keys: [1, 2, 3], | |
printName: function() { | |
this.keys.forEach(function(x) { | |
console.log(this.name); | |
}) | |
} | |
} |
View arrowDefined.js
This file contains 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 user = { | |
name: "Ahmar", | |
keys: [1, 2, 3], | |
printName: function() { | |
this.keys.forEach((x) => { | |
console.log(this.name); | |
}) | |
} | |
} |
View arrowFunctions.js
This file contains 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 user = { | |
name: "Ahmar", | |
printName: () => { | |
console.log(this.name); | |
} | |
} | |
user.printName(); //Outputs: Undefined |
View class.js
This file contains 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
class User { | |
constructor(name) { | |
this.name = name; | |
} | |
printName() { | |
console.log(this.name); | |
} | |
} |
View bind.js
This file contains 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 user = { | |
name: "Ahmar", | |
printName: function() { | |
console.log(this.name); | |
} | |
} | |
const printNameOutsideOfObject = user.printName | |
const boundFunction = printNameOutsideOfObject.bind(user); | |
boundFunction(); //Outputs: Ahmar |
View printNameOutsideOfObject.js
This file contains 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 printNameOutsideOfObject = function() { | |
console.log(this.name); | |
} | |
printNameOutsideOfObject(); //The "this" here refers to the global window object, and not the user object. |
View objectFunction.js
This file contains 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 user = { | |
name: "Ahmar", | |
printName: function() { | |
console.log(this.name); | |
} | |
} | |
const printNameOutsideOfObject = user.printName | |
printNameOutsideOfObject(); //Error, cannot read property of undefined. |
View printName.js
This file contains 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 user = { | |
name: "Ahmar", | |
printName: function() { | |
console.log(this.name); | |
} | |
} | |
user.printName(); //Outputs: "Ahmar" |