码迷,mamicode.com
首页 > 其他好文 > 详细

个人思考:能否sub.prototye=sup.prototype实现继承

时间:2016-05-23 00:51:19      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

var Sup=function(name){
	this.name=name;
};
var Sub=function(name){
	this.name=name;
};
Sup.prototype.sayName=function(){
	alert(this.name);
}
Sub.prototype=Sup.prototype;
Sup.prototype.sayAge=function(){
	alert(21);
}
Sub.prototye.sayMe=function(){
	alert("me");
}
var instance=new Sub("gao");
instance.sayName();		//gao	
instance.sayAge();	//21
instance.sayMe();   //不能执行,sayMe函数不能被定义

  Sub.prototype=Sup.prototype是可以执行的,同时也实现了继承,在Sup.prototype中添加方法可以成功,而在Sub.prototype添加方法会失败。如果仅仅是想继承Sup.prototype的方法,而不用添加Sub.prototype自己的方法,完全可以使用这样的方式实现继承。

 

个人思考:能否sub.prototye=sup.prototype实现继承

标签:

原文地址:http://www.cnblogs.com/pick7/p/5518391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!