经过原型继承javascript能够模仿实现实现java中继承的概念。 javascript
var Person = function(name) { this.name = name; }; Person.prototype.getName = function() { return this.name; }; var Customer = function(name) { this.name = name; };
既然既然原型prototype也是一个对象,那么能够给Customer的prototype赋值一个对象,这个对象的属性和方法将被new出来的Customer对象继承。 java
Customer.prototype = new Person();//Customer继承Person var c = new Customer("kimi"); alert(c.getName());//结果输出kimi能够为Customer的原型新加属性方法,但又不会改变Person的原型
Customer.prototype.setAmountDue = function(amountDue) { this.amountDue = amountDue; }; c.setAmountDue;//结果为function对象 Person.prototype.setAmountDue;//结果为undefined