<html>
<head>
</head>
<body>
<script type="text/javascript">
//继承之对象冒充方式,可以继承多个父类
function user(name){
this.name = name;
this.sayname = function(){
alert(this.name);
}
}
function student(name,score){
//user(name);
this.temp = user;
this.temp(name);
delete this.temp;
this.score = score;
this.sayscore = function(){
alert(this.score);
}
}
var s = new student('tom',33);
//s.sayname();
//s.sayscore();
//用call()函数实现继承
function user(name){
this.name = name;
this.sayname = function(){
alert(this.name);
}
}
function student(name,score){
user.call(this,name);
this.score = score;
this.sayscore = function(){
alert(this.score);
}
}
var s = new student('tom',33);
//s.sayname();
//s.sayscore();
//用apply()函数实现继承
function user(name){
this.name = name;
this.sayname = function(){
alert(this.name);
}
}
function student(name,score){
user.apply(this,[name]);
this.score = score;
this.sayscore = function(){
alert(this.score);
}
}
var s = new student('tom',33);
//s.sayname();
//s.sayscore();
//原型链方式实现继承
//1.将父类中所用通过prototype设置的属性或方法放到子类中
//2.并覆盖子类中所以的prototype的设置
//3.所以子类自己的所以的prototype的设置要放在继承父类的下面
//4.缺点是不支持多个继承,构造函数不能有参数
function user(){}
user.prototype.name = '';
user.prototype.say = function(){
alert(this.name);
}
function student(){}
student.prototype =new user();
student.prototype.age = 0;
student.prototype.show = function(){
alert(this.age);
}
var s = new student();
s.name = 'tom';
s.age = 44;
//s.say();
//s.show();
//alert(s instanceof user);
//alert(s instanceof student);
//混合模式实现继承
function user(name){
this.name = name;
}
user.prototype.sayname = function(){
alert(this.name);
}
function student(name){
user.call(this.name);
}
//将user中所有通过prototype设置的方法放到student中
student.prototype = new user();
student.prototype.age = 0;
student.prototype.sayage = function(){
alert(this.age);
}
var s = new student();
s.name = 'tom';
s.age = 44;
s.sayname();
s.sayage();
alert(s instanceof user);
alert(s instanceof student);
</script>
</body>
<html/>