-
-
Save digitalconceptvisuals/44117b121201b6220b89366e578e2cd3 to your computer and use it in GitHub Desktop.
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
/* | |
If a function name starts with uppercase | |
It is "intended" to be used as a constructor | |
Imagine a "class" around it, it will make sense | |
*/ | |
// class Person { | |
function Person(id, name) { | |
this.id = id; | |
this.name = name; | |
} | |
// } | |
// Now let's create the object | |
let eich = new Person(1, "Brendan"); | |
console.log(eich.id, eich.name); | |
// OOPS.. we called is like a function | |
// But there is no error | |
let brendan = Person(2, "Wrongdon"); | |
// Why error? where did id and name go? | |
console.log(brendan.id, brendan.name); | |
-------------------------------------- | |
Output | |
-------------------------------------- | |
console.log(brendan.id, brendan.name); | |
^ | |
TypeError: Cannot read property 'id' of undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment