js零碎知识:继承-extends

//静态方法、属性
//定义一个结构体
function People (){}

People.name = 'aaa';
People.findData = function(){
	alert("staticMethod");
}
//alert(People.name);
//People.findData();

//--------------------------继承开始

//继承
//var A = function(){}
function A(v){
	this.v = v;
	//alert("i coming1302");
	this.show = function(){
		alert(this.v);
	}
}

A.prototype = {//用法:参考HashMap

}
A.prototype.setV = function(){
	this.v = 123123;
}
var a = new A(111);
//a.setV();//把111用123123给覆盖掉了
//var b  = new A(888);
//a.show();//如果上面加了var b = new A(888);也是不会覆盖111的


//1.构造继承
function C(){
	this.sss = A;//将A的地址赋予给sss;
	this.sss("构造继承");//这个将会调用A这个方法
	/*this.show = function(){//重写
		alert("xxx");
	}*/
	delete this.sss;

}

var c = new C();
//如果在C中没有show方法   就调用A里的show方法,否则就调用C自己的show方法
//c.show();

//解释delete this.sss;
//var cc = c.sss;
//在这里我们又拿到了A的结构体,
//为了避免A的结构体继承往下通过C去传递,delete this.sss;
//alert(cc);

//可以实现多继承
function X(){
	this.xxx = C;
	this.xxx();

}
var x = new X();
//x.show();

//2.冒充继承 call  父类.call(子类实例对象,参数列表);
function A(v,n){
	this.v = v;
	this.n = n;
	//alert("i coming1302");
	this.show = function(){
		alert(this.v+this.n);
	}
}
//子类
function D(){
	//var d = new D();
	//A.call(d,"call继承","sss");//错误

	A.call(this,"call继承","sss");
}

var d = new D();
A.call(d,"call继承","sss");//参数列表

//d.show();

//var d1 = new D();//如果用A.call(d,paramers);这个的话,d1就没有继承可言
//d1.show();


//alert(d.v);


//3.冒充继承 apply  父类.apply(子类实例对象,数组);
function A(v,n){
	this.v = v;
	this.n = n;
	//alert("i coming1302");
	this.show = function(){
		alert(this.v+this.n);
	}
}
//子类
function E(){
	A.apply(this,["call继承","sss"]);
}

var e = new E();
//A.apply(e,["call继承","sss"]);

//e.show();


//4.原型继承
function A(v,n){
	this.v = v;
	this.n = n;
	//alert("i coming1302");
	this.show = function(){
		alert(this.v+this.n);
	}
}
function F(){

}

F.prototype = new A('111','222');//父类的实例指向子类的原型
var f= new F();
//f.show();


// .调用  和   []调用的区别 
/*
var s = {
	m1: function(){alert("m1111")},
	m2:function(){alert("m2222")}
}
//alert(s.id);
var n = "1";
if(n=="s1"){
	s.m1();
}else if(n=="t1"){
	s.m2();
}
s[n]();
for(var parm in s){
	alert(s[parm]);
}*/


//5.拷贝继承
//父类
var H = function(){
	//定义一个空的结构体
}
H.prototype = {
	m1: function(){alert("m1111")},
	m2:function(){alert("m2222")}
}
//子类
var K = function(){}
K.id ="" ;

for(var parm in H.prototype){
	// parm ===> m1  m2
      //完全错误(因为H.prototype下没有pram那个方法或属性)
	//K.parm = H.prototype.parm; 
	//K.parm = H.prototype[parm];//  静态调用 ---111 
	//222 这种写法就是最终m2 去覆盖m1  赋予给K.prototype的parm这个方法 
	//K.prototype.parm = H.prototype[parm];
      // 333   这种写法下 K.prototype下有  m1  m2   方法 
	K.prototype[parm] = H.prototype[parm];
}

var k = new K();
//k.m1();
//k.m2();
//k.parm();//222可调用   如果333的话  就没有这个方法 

//K.parm();//  静态调用 ---111
//k.m1();



//6.extends 
var Base ={};

Base.extend = function (src,dist){
	for(var i in src){
		if(src[i]!=undefined){
			dist[i] = src[i];
		}
	}
}

var ddd = {}

var sss ={
	id : "101",
	name : 'sz',
	shows:function(){
		alert("over...");
	}
}

Base.extend(sss,ddd);

ddd.shows();

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值