object function js code example

Example 1: javascript check object methods

Object.getOwnPropertyNames()

Example 2: js object

let car = {
  		name: "BMW",
  		colour: "black",
  		year: "2020",
  		owner: {
		names = ["Andy" , "Steve" , "Tony" ]
		}
};

Example 3: 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 4: js objects

// To make an object literal:
const dog = {
    name: "Rusty",
    breed: "unknown",
    isAlive: false,
    age: 7
}
// All keys will be turned into strings!

// To retrieve a value:
dog.age; //7
dog["age"]; //7

//updating values
dog.breed = "mutt";
dog["age"] = 8;

Example 5: adding function to objects js

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

Example 6: js create object with properties

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

Tags:

Misc Example