Skip to content

Instantly share code, notes, and snippets.

@moisescastillo
Created July 19, 2020 21:42
Show Gist options
  • Save moisescastillo/1f8893a4dbdbba22fb6d81b048d3428b to your computer and use it in GitHub Desktop.
Save moisescastillo/1f8893a4dbdbba22fb6d81b048d3428b to your computer and use it in GitHub Desktop.
Javascript, the constructor property
function Person(name) {
this.name = name;
this.greeting = function() {
console.log(`Hi I'm ${this.name}`);
}
}
let person1 = new Person('Peter');
let person2 = new person1.constructor('Sarah');
console.log(person2.name);
// -> "Sarah"
person2.greeting();
// -> "Hi I'm Sarah"
console.log(person2.constructor.name);
// -> "Person"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment