DEV Community

Discussion on: HOW TO CREATE OBJECTS IN JAVASCRIPT

Collapse
 
salaheddin12 profile image
Salah Eddine Ait Balkacm

This is a constructor that creates an instance of a Person

function Person(name, age) {
  this.name = name;
  this.age = age;
  this.Greeting = function() {
    // ...
  }
} 

And this is a factory fuction that creates an object of type personFF by returning an object with the properties geven as parameters

function personFF(name, age) { //here age and name can be named anything.
//this (name_ and age_) is just a naming convention I used :)
  return 
  //this returns an object using the object literal.
  //so the object that we return can have as many properties and methods depending on our //needs.
  {
    name, 
    age,
    //we can add methods and properties here
  };
}