Skip to content

Instantly share code, notes, and snippets.

@kironroy
Last active July 25, 2023 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kironroy/72ec9c55b40487f45ead18649bad4174 to your computer and use it in GitHub Desktop.
Save kironroy/72ec9c55b40487f45ead18649bad4174 to your computer and use it in GitHub Desktop.
constructor functions
'use strict';
// constructor functions
// 1. New {} is created
// 2. function is called, this = {}
// 3. {} linked to prototype
// 4. function automatically return {}
const Person = function (firstName, birthYear) {
// instance properties
this.firstName = firstName;
this.birthYear = birthYear;
// NEVER CREATE A METHOD INSIDE A CONSTRUCTOR fX
// Performance issues
// this calcAge = function() {
// console.log(2037 - this.birthYear);
// }
};
const jonas = new Person('Jonas', 1991);
console.log(jonas);
const zelda = new Person('Zelda', 2022);
const grace = new Person('Grace', 1964);
console.log(zelda, grace);
const Terry = 'Terry';
console.log(jonas instanceof Person);
console.log(Terry instanceof Person);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment