Skip to content

Instantly share code, notes, and snippets.

@Goloburda
Created November 26, 2019 08:35
Show Gist options
  • Save Goloburda/497bf5526b91164cff23e232ce1eb3be to your computer and use it in GitHub Desktop.
Save Goloburda/497bf5526b91164cff23e232ce1eb3be to your computer and use it in GitHub Desktop.
Keyword "this". JavaScript.
"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