Difference between revisions of "Defining Objects"

From rbachwiki
Jump to navigation Jump to search
Line 1: Line 1:
== Objects ==
<pre>
<pre>
var john = {
var john = {
     name: 'John',
     name: 'John',
     lastName: 'Smith',
     lastName: 'Smith',
Line 7: Line 8:
     isMarried: false,
     isMarried: false,
     family: ['jane', 'mark'],
     family: ['jane', 'mark'],
     calculateAge: function(yob) {
     calculateAge: function() {
         return 2016 - yob;
         return 2016 - this.yearOfBirth;
     }
     }
};
};
 
console.log("--- " + john.calculateAge());
console.log(john.calculateAge(1990));
</pre>
</pre>
===Loop through items===
===Loop through items===
Line 18: Line 18:
  console.log(item + " - " + john [item]);
  console.log(item + " - " + john [item]);
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
== Create an Object with a calculated value within the object ==
'''this will create the john.age with the calculated value'''
<pre>
var john = {
    name: 'John',
    lastName: 'Smith',
    yearOfBirth: 1990,
    job: 'Teacher',
    isMarried: false,
    family: ['jane', 'mark'],
    calculateAge: function() {
        this.age = 2016 - this.yearOfBirth;
    }
};
</pre>

Revision as of 23:56, 1 November 2016

Objects

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]);

Back To Top- Home - Category

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;
    }
};