js组合继承

本文探讨了JavaScript中的三种继承实现:原型链继承、构造函数继承和组合继承。通过示例代码展示了每种继承方式的工作原理,特别是原型链继承中关于引用类型属性的修改和覆盖行为,以及如何在构造函数继承中避免重复调用父类构造函数的问题。组合继承则综合了前两者的优势,但同时也存在冗余的原型链问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原型链继承:
            //原型对象上的任何类型的值,都不会被实例所重写/覆盖,
            //在实例上设置语原型对象上同名属性的值,只会在实例上创建一个同名的本地属性
            //但是是引用类型的就会修改 

//原型链继承
			function Person(name){
				this.name='dfdf'
			}
			Person.setName=function(name){
				this.name=name
			}
			Person.prototype.setName=function(name){
				this.name=name
			}
			Person.prototype.nmae=[{naem:'eeee'}]
			function sun(name){
				this.name3=name
			}
			sun.prototype=new Person()
			sun.prototype.say=function(){
				console.log('wo'+this.name3)
			}
			var su=new sun('rr')
			//su.setName('eee')
			su.say()
			su.nmae.push({naem:'ee3333333ee'})
			var suu=new sun('444')
			

 

//构造函数继承
			
			function Person(name){
				this.name=['dfdsafds']
			}
			Person.setName=function(name){
				this.name=name
			}
			Person.prototype.setName=function(name){
				this.name=name
			}
			Person.prototype.nmae=[{naem:'eeee'}]
			function sun(name){
				this.name3=name
				Person.call(this)
			}
			//sun.prototype=new Person()
			sun.prototype.say=function(){
				console.log('wo'+this.name3)
			}
			var su=new sun('rr')
			//su.setName('eee')
			su.say()
			su.nmae=[{naem:'ee3333333ee'}]
			var s3uu=new sun('444')

 

//组合继承
			
			function Person(name){
				this.name=['dfdsafds']
			}
			Person.setName=function(name){
				this.name=name
			}
			Person.prototype.setName=function(name){
				this.name=name
			}
			Person.prototype.nmae=[{naem:'eeee'}]
			function Sun(name){
				this.name3=name
				Person.call(this)
			}
			Sun.prototype=new Person()
			Sun.prototype.constructor=Sun
			var su=new Sun('d')
			su.nmae=[{naem:'eeee3333'}]
			var su2=new Sun('d')

 

 

 


			function inheritPrototype(subType, superType){
			    //原型式继承:浅拷贝superType.prototype对象作为superType.prototype为新对象的原型
			    // 内部会自带_proto_指向:prototype.\_\_proto\_\_ = superType.prototype;
			    var prototype = Object.create(superType.prototype); 
			    // subType.prototype.\_\_proto\_\_ = superType.prototype;
			    subType.prototype = prototype;               // 将子类的原型替换为这个原型
			    prototype.constructor = subType;             // 修正原型的构造函数
			    
			}
			
			function SuperType(name){
			    this.name = name;
			    this.colors = ["red", "blue", "green"];
			}
			
			SuperType.prototype.sayName = function(){
			    alert(this.name);
			};
			
			function SubType(name, age){
			    SuperType.call(this, name);
			    this.age = age;
			}
			// 核心:因为是对父类原型的复制,所以不包含父类的构造函数,也就不会调用两次父类的构造函数造成浪费
			inheritPrototype(SubType, SuperType);
			SubType.prototype.sayAge = function(){
			    alert(this.age);
			}
			var nu=new SubType('33',33)
			var nu2=new SubType('334',334)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值