几种js的继承方式

//1、继承第一种方式:对象冒充 
function Super(username){
	this.username = username;
	this.hello = function(){
		alert(this.username);
	};
}

function Sub(username){
	this.super = Super;
	this.super(username);
	delete this.super;
	this.world = function(){
		alert(this.username+" Sub");
	};
}

var superNew = new Super("super");
var subNew = new Sub("sub");
superNew.hello();
subNew.hello();
subNew.world();
//2、继承第二种方式:call()方法方式 
/*
  call方法是Function类中的方法 
  call方法的第一个参数的值赋值给类(即方法)中出现的this 
  call方法的第二个参数开始依次赋值给类(即方法)所接受的参数
*/
function TestCall(str){ 
    alert(this.name + " " + str); 
  } 
  var object = new Object(); 
  object.name = "upxiaofeng"; 
  TestCall.call(object," bye bye");
 
function Super(){
	this.show=function(){
		alert("super");
	};
}

function Sub(){
	Super.call(this);
}

var superCall = new Super();
superCall.show();
var sbuCall = new Sub();
sbuCall.show();
//3、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承 
function Super(){

}
Super.prototype.hello = "hello";
Super.prototype.sayHello = function(){
	alert(this.hello);
};
function Sub(){

}
Sub.prototype = new Super();
Sub.prototype.world = "world";
Sub.prototype.sayWorld = function(){
	alert(this.world);
};
var client = new Sub();
client.sayHello();
client.sayWorld();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值