object inside an object javascript code example

Example 1: function inside object javascript

var obj = {
  func: function(a, b) {
    return a * b;
  }
};

obj.func(3, 6); // 18

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: object inside object javascript

var obj = {
  prop1: 5,
  obj2: {
    prop1: [3, 6, 3],
    prop2: 74,
    prop3: {
      str: "Hello World"
    }
  }
};

console.log(obj.obj2.prop3.str); //output: "Hello World"

Example 4: adding function to objects js

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

Example 5: js create object with properties

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

Tags:

Java Example