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