1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| function Parent(username){ this.username = username; this.hello = function(){ console.log(this.username); } }
Parent.prototype.sayMorning = function(){ console.log('good morning ' + this.username); }
function Child(username,password){ Parent.call(this,username);
this.password = password; this.world = function(){ console.log(this.password); } }
var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); parent.sayMorning(); child.hello(); child.world(); // child.sayMorning(); 通过prototype 添加的方法和属性,不能用来继承
|