Created
November 26, 2019 08:35
-
-
Save Goloburda/497bf5526b91164cff23e232ce1eb3be to your computer and use it in GitHub Desktop.
Keyword "this". JavaScript.
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
"use strict"; | |
console.log(this); //undefined | |
function Person(name, age) { | |
this.name = name; | |
this.age = age; | |
this.sayName = () => { | |
console.log(this.name); | |
}; | |
} | |
/* | |
The "new" operator lets developers create an instance | |
of a user-defined object type | |
*/ | |
const person = new Person("Mikita", 25); | |
person.sayName(); // "Mikita" | |
const tree = { | |
age: 200, | |
sayAge: function() { | |
console.log(this.age); | |
} | |
}; | |
tree.sayAge(); // 200 | |
tree.sayAge.call(person); // 25 | |
const personSayAge = tree.sayAge.bind(person); | |
personSayAge(); //25 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment