By: Tristan Pedersen
Constructor and literals behave very differently in JS. A Constructor is a function that creates an instance of a class which is typically called an 'object'. This makes it easy to create multiple objects using the same blueprint. An example of this:
function Person(first, last, age) { this.firstName = first; this.lastName = last; this.age = age; } const example = new Person("Tyler", "Pedersen", 50, "blue"); const newExample = new Person("Sally", "Rally", 48, "green");
We were able to make multiple objects by passing them to our constructor function; using it as a blueprint. Constructor uses function declaration type syntax and literal notation uses the variable declaration type syntax.
Objects created using literals are normally meant to be created one time; this is because when a change is made to the object, it affects that object across the entire scope. An example of this:
let Person={ person_name:"Example" person_age:'200' }
Summary: If you want to create objects of different type; the Object Literal is useful. If you want to create object of the same type then you can use the Constructor Function.