JavaScript继承的几种实现方式?

版权声明:本文为博主原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_44721831/article/details/102642437

Javascript如何实现继承?

1、构造继承
2、原型继承
3、实例继承
4、拷贝继承

原型prototype机制或apply和call方法去实现较简单,建议使用构造函数与原型混合方式。

    function Parent(){                                  
		this.name = 'wang';                             
	}                                                   
                                                        
	function Child(){                                   
		this.age = 28;                                  
	}                                                   
	Child.prototype = new Parent();//继承了Parent,通过原型     
                                                        
	var demo = new Child();                             
	alert(demo.age);                                    
	alert(demo.name);//得到被继承的属性                         

JavaScript继承的几种实现方式?

猜你喜欢

转载自blog.csdn.net/qq_44721831/article/details/102642437