今天来说说javascript中的继承. 我将介绍几种继承的方法:this.base,call,apply,prototype
同样来看代码吧.
第一部分: 使用this.base实现继承.支持派生类调用基类带参构造函数
同样来看代码吧.
第一部分: 使用this.base实现继承.支持派生类调用基类带参构造函数
<script type="text/javascript">
function Person(name,age) {
this._name = name;
this._age = age;
}
function Man(name,age) {
this._sex = "男";
this.base = Person;
this.base(name,age);
}
var m1 = new Man("小a",18);
document.writeln(m1._name);//小a
document.writeln(m1._age);//18
document.writeln(m1._sex);//男
</script>
function Person(name,age) {
this._name = name;
this._age = age;
}
function Man(name,age) {
this._sex = "男";
this.base = Person;
this.base(name,age);
}
var m1 = new Man("小a",18);
document.writeln(m1._name);//小a
document.writeln(m1._age);//18
document.writeln(m1._sex);//男
</script>
运行结果:小a 18 男
噢,不错.成功喽. this.base = Person 很清晰的指明了基类, this.base(name,age)调用基类构造函数
第二部分: 使用prototype实现继承,不过很遗憾它不支持派生类调用基类带参构造函数
<script type="text/javascript">
function Person(name,age) {
this._name = name;
this._age = age;
this._height = 1.7;
}
function Man(name,age) {
this._sex = "男";
}
Man.prototype = new Person();
var m1 = new Man("小a",18);
document.writeln(m1._name);//undefined
document.writeln(m1._age);//undefined
document.writeln(m1._height);//1.7
document.writeln(m1._sex);//男
</script>
function Person(name,age) {
this._name = name;
this._age = age;
this._height = 1.7;
}
function Man(name,age) {
this._sex = "男";
}
Man.prototype = new Person();
var m1 = new Man("小a",18);
document.writeln(m1._name);//undefined
document.writeln(m1._age);//undefined
document.writeln(m1._height);//1.7
document.writeln(m1._sex);//男
</script>
运行结果: undefined undefined 1.7 男
m1._height输出1.7说明Man成功继承了Person.
可是m1._name和m1._age输出undefined说明,new Man("小a",18)没有把参数传到Person中,以失败告终.
不过针对基类是无参构造函数使用prototype还是还有效
第三部分: 使用call 实现继承,不过可称之为伪继承
<script type="text/javascript">
function Person(name,age) {
this._name = name;
this._age = age;
}
function Man(name,age) {
this._sex = "男";
Person.call(this,name,age);
}
var m1 = new Man("小a",18);
document.writeln(m1._name);//小a
document.writeln(m1._age);//18
document.writeln(m1._sex);//男
</script>
function Person(name,age) {
this._name = name;
this._age = age;
}
function Man(name,age) {
this._sex = "男";
Person.call(this,name,age);
}
var m1 = new Man("小a",18);
document.writeln(m1._name);//小a
document.writeln(m1._age);//18
document.writeln(m1._sex);//男
</script>
运行结果:小a 18 男
call()它的第一个参数是this指向的对象,所有的其他参数都直接传到function
第四部分: 使用apply实现继承,不过可称之为伪继承
<script type="text/javascript">
function Person(name,age) {
this._name = name;
this._age = age;
}
function Man(name,age) {
this._sex = "男";
Person.apply(this,new Array(name,age));
}
var m1 = new Man("小a",18);
document.writeln(m1._name);//小a
document.writeln(m1._age);//18
document.writeln(m1._sex);//男
</script>
function Person(name,age) {
this._name = name;
this._age = age;
}
function Man(name,age) {
this._sex = "男";
Person.apply(this,new Array(name,age));
}
var m1 = new Man("小a",18);
document.writeln(m1._name);//小a
document.writeln(m1._age);//18
document.writeln(m1._sex);//男
</script>
运行结果:小a 18 男
apply()方法需要两个参数:this所指向的对象,和传到function的由参数组成的array