Last active
July 25, 2023 18:29
-
-
Save kironroy/72ec9c55b40487f45ead18649bad4174 to your computer and use it in GitHub Desktop.
constructor functions
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'; | |
// 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