Using Object literal to create object - Node.js Object

Node.js examples for Object:Object Operation

Description

Using Object literal to create object

Demo Code

var name = 'Johan';

var person = {//from   w w  w.  ja v a 2 s.c  om
    'first-name': 'Isak',
    age: 34,
    details: {
        hobbies: ['kitesurfing', 'family'],
        location: 'Sweden'
    },
    greet: function() {
        console.log('Hello, I am ' + this.age + ' years old');
    }
};

console.log(person);
console.log(person.name);
console.log(person['name']);

var field = 'name';
console.log(person[field]);

person.greet();
console.log(person.details.hobbies);
console.log(person.details.hobbies[0]);
console.log(typeof person['first-name']);
console.log(person['first-name']);

person['first-name'] = 'Anders';

console.log(person['first-name']);
person.greet();


var age = 34;

Related Tutorials