javascript new person object code example

Example 1: javascript constructor function

function ClassMates(name,age){
  this.name=name;
  this.age=age;
  this.displayInfo=function(){
    return this.name + "is " + this.age + "year's old!";
  }
}

let classmate1 = new ClassMates("Mike Will", 15);
classmate.displayInfo(); // "Mike Will is 15 year's old!"

Example 2: object object javascript

person = {
    'name':'john smith'
    'age':41
};

console.log(person);
//this will return [object Object]
//use
console.log(JSON.stringify(person));
//instead

Example 3: keyword new js

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
//create a object with three keys, make, model, and year

var myCar = new Car('Eagle', 'Talon TSi', 1993);
// use the new operator to create any number of car objects with this template object Car above

Tags:

Java Example