Difference between revisions of "Constructors"

From rbachwiki
Jump to navigation Jump to search
Line 2: Line 2:
=Constructor=
=Constructor=
<pre>
<pre>
function Person(name, age){
function Balance(current, deposit){
   this.name = name;
   this.current = current;
   this.age=age;
   this.deposit = deposit;
   this.display=function(){
   this.display=function(){
     console.log("My name is " + name);
     current +=deposit;
     console.log("My age is "+ age);
     console.log(current);
   }
   }
}
}
// create a new Object
var balance = new Balance(100,24);
var person = new Person("robert",24);
balance.display();
person.display();
</pre>
</pre>

Revision as of 23:00, 20 October 2016

Constructor

function Balance(current, deposit){
  this.current = current;
  this.deposit = deposit;
  this.display=function(){
    current +=deposit;
    console.log(current);
  }
}
var balance = new Balance(100,24);
balance.display();