I'm looking to analyze this code and I'm having some trouble. My trouble starts with this line. Customer.prototype = new Person();
. Now as far as i'm aware. We can add some methods to our variables like we did with Person.prototype.getName
. Great, now Person
has a proto
pointed towards a function that returns a name. So what does it mean when we do Customer.prototype = new Person();
. Does this mean that take all the methods and statements in Person
and put them inside the variable Customer
?
var Person = function(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var john = new Person("John");
//Try the getter
alert(john.getName());
Person.prototype.sayMyName = function() {
alert('Hello, my name is ' + this.getName());
};
john.sayMyName();
var Customer = function(name) {
this.name = name;
};
Customer.prototype = new Person();
var myCustomer = new Customer('Dream Inc.');
myCustomer.sayMyName();
Customer.prototype.setAmountDue = function(amountDue) {
this.amountDue = amountDue;
};
Customer.prototype.getAmountDue = function() {
return this.amountDue;
};
myCustomer.setAmountDue(2000);
alert(myCustomer.getAmountDue());