Defining Objects

From rbachwiki
Revision as of 21:03, 6 November 2016 by Bacchas (talk | contribs)
Jump to navigation Jump to search

Object create

var john = {
    name: 'John',
    lastName: 'Smith',
    yearOfBirth: 1990,
    job: 'Teacher',
    isMarried: false,
    family: ['jane', 'mark'],
    calculateAge: function() {
        return 2016 - this.yearOfBirth;
    }
};
console.log("--- " + john.calculateAge());

Loop through items

for(var item in john ){
console.log(item + " - " + john [item]);


Create an Object with a calculated value within the object

this will create the john.age with the calculated value

var john = {
    name: 'John',
    lastName: 'Smith',
    yearOfBirth: 1990,
    job: 'Teacher',
    isMarried: false,
    family: ['jane', 'mark'],
    calculateAge: function() {
        this.age = 2016 - this.yearOfBirth;
    }
};

Creating a new Object for the john object

var harry = Object.create(john);

Back To Top- Home - Category