Difference between revisions of "Constructors"

From rbachwiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:


=Constructor=
=Function Constructor=
<pre>
<pre>
function Balance(current, deposit){
var Person = function(name, yearOfBirth, job){
  this.current = current;
    this.name = name;
  this.deposit = deposit;
    this.yearOfBirth = yearOfBirth;
  this.display=function(){
    this.job = job;
    current +=deposit;
      
     console.log(current);
  }
}
}
var balance = new Balance(100,24);
 
balance.display();
 
var john = new Person('john', 1967, 'teacher');
john.calculateAge();
 
</pre>
== Adding Inheritance ==
'''The calculateAge was taken out of the constructor and added to the protoype so other instances of person could use it without it being a part of the person function constructor.'''
<pre>
Person.protoytpe.calculateAge = function() {
        console.log(2016 - this.yearOfBirth);
    }
</pre>
</pre>
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==

Latest revision as of 17:20, 6 November 2016

Function Constructor

var Person = function(name, yearOfBirth, job){
    this.name = name;
    this.yearOfBirth = yearOfBirth;
    this.job = job;
    
}


var john = new Person('john', 1967, 'teacher');
john.calculateAge();

Adding Inheritance

The calculateAge was taken out of the constructor and added to the protoype so other instances of person could use it without it being a part of the person function constructor.

Person.protoytpe.calculateAge = function() {
        console.log(2016 - this.yearOfBirth);
    }

Back To Top- Home - Category