call an object javascript code example

Example 1: set variable to object without editing old object js

// Deep Clone
obj = { a: 0 , b: { c: 0}};
let deepClone = JSON.parse(JSON.stringify(obj));

Example 2: set variable to object without editing old object js

// [Object1]<--------- myObj

const tempMyObj = Object.assign({}, myObj);

// [Object1]<--------- myObj
// [Object2]<--------- tempMyObj

Example 3: creating an object javascript

let obj = {
// fields
  name:"value",
  num: 123,
//methods
  foo: function(){}
  
}

Example 4: object literal javascript

// Object literals are defined using the following syntax rules:
// A colon separates property name from value.
// A comma separates each name-value pair from the next.
// There should be no comma after the last name-value pair.

// If any of the syntax rules are broken, such as a missing comma or colon or curly brace,
// an error will be thrown.

var myObject = {
    sProp: 'some string value',
    numProp: 2,
    bProp: false
};

Tags:

Php Example