Below is a working example to show how the prototype of Javascript work. So from my understand is simply the customer instance inherited the prototype function of the Person.
var Person = function (name) { this.name = name; }; Person.prototype.getName = function () { return this.name; }; var john = new Person("John"); 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());
But one thing bug me is that why the author do a getAmountdue prototype function? it simply return this.amountDue.
I think the issue is not related to javascript prototypical inheritance.
To wrap the property into a function return could abstract the implementation.
For example, if your boss want you provide a decorated result of this.amountDue
, you can just change the implementation of getAmountDue
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.