javascript继承方式.
Posted Samsara315
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript继承方式.相关的知识,希望对你有一定的参考价值。
//原型链继承
function Parent() {
this.name = ‘per‘;
}
function Child() {
this.age = 20;
}
Child.prototype = new Parent();
var child = new Child();
console.log(child.name + " " + child.age)//per 20
//构造函数继承
function Parent(age) {
this.name = ‘person‘;
this.age = age;
}
function Child(age) {
Parent.call(this, age)
}
var child = new Child(20)
console.log(child.name + " " + child.age)//per 20
// 组合继承
function Parent(age) {
this.name = ‘per‘;
this.age = age;
}
Parent.prototype.sayAge = function () {
return this.name + ‘ age is ‘ + this.age;
}
function Child(age) {
Parent.call(this, age)
}
Child.prototype = new Parent();
var child = new Child(21);
console.log(child.sayAge())//per age is 21
以上是关于javascript继承方式.的主要内容,如果未能解决你的问题,请参考以下文章