Difference between revisions of "Defining Objects"

From rbachwiki
Jump to navigation Jump to search
Line 1: Line 1:
== Objects ==
== Object create ==
<pre>
<pre>
var john = {
var john = {
Line 17: Line 17:
  for(var item in john ){
  for(var item in john ){
  console.log(item + " - " + john [item]);
  console.log(item + " - " + john [item]);
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
 


== Create an Object with a calculated value within the object ==  
== Create an Object with a calculated value within the object ==  
Line 35: Line 35:


</pre>
</pre>
'''Creating a new Object for the john object'''
var harry = Object.create(john);
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==

Revision as of 21:03, 6 November 2016

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