js construct object code example

Example 1: js constructor object

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

var car1 = new Car('Eagle', 'Talon TSi', 1993);

console.log(car1.make);
// expected output: "Eagle"

Example 2: javascript constructor object

function Person(first, last, age, eye) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eye;
}

Example 3: adding function to objects js

var myObj = {
	myFunc: function(param){
      //do stuff
    }
}

Example 4: js create object with properties

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Tags:

Java Example