Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Created July 29, 2020 15:25
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 digitalconceptvisuals/44117b121201b6220b89366e578e2cd3 to your computer and use it in GitHub Desktop.
Save digitalconceptvisuals/44117b121201b6220b89366e578e2cd3 to your computer and use it in GitHub Desktop.
/*
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