Thursday, 14 July 2016

Javascript object is name value collection

In Javascript, an object is a name value collection.

// the shorthand to declare an new object,.
var o = {};


// set the age to 18.
o['age'] = 18;

// you have 2 ways to retrieve the age:
// property style:
console.log(o.age);

// name value collection style:
console.log(o['age']);

// you may also assigning the value with the "name" that can't be access in "property" style.
o['abc-1'] = 1;
o['name 1'] = 2;

console.log(o['abc-1']);
console.log(o['name 1']);

No comments:

Post a Comment

We are moving

We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm