`
kidiaoer
  • 浏览: 808090 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

js继承

阅读更多
js继承

第一种继承var A = {name:"diaoer",show:function(){
		alert(this.name);
	}
}
var B = {};
for (var i in A) {
	B[i]=A[i];
}
B.show();


第二种继承function A() {
	this.name="diaoers";
	this.show=function(){
		alert(this.name);
	}
}
function B(){
	this.b=A;
	this.b();
	delete this.b;
}
var o = new B();
o.show();




第三种继承function A(name) {
	this.name=name;
	this.show=function(){
		alert(this.name);
	}
}
function B(){
	A.call(this,'张三');//它的参数是一个一个传的  而例外一种方式就是通过apply方法来创建 它 后面的参数是一个数组或者argments对象
}
var o = new B();
o.show();


第四种继承function A(name) {
	this.name=name;
	this.show=function(){
		alert(this.name);
	}
}
function B(){
	A.apply(this,['张三']);
}
var o = new B();
o.show();



第五种方式
function A(name) {
	this.name=name;
	this.show=function(){
		alert(this.name);
	}
}
function B(){
	
}
B.prototype=new A("diaoer");
var o = new B();
o.show();

分享到:
评论
1 楼 lanzhengwu 2011-12-12  
非常不错。。

相关推荐

Global site tag (gtag.js) - Google Analytics