[Javascript] 继承 (原型继承, 类继承)
来源:互联网 发布:蜀山战纪 网络结局 编辑:程序博客网 时间:2024/05/02 04:26
(1) 原型链继承
在js中没有类的概念, 所以继承的话, 使用原型链继承
B.prototype=new A();
4步:[1] 初始化对象[2] B.prototype=A.prototype[3] A作用域给B A.call(B.prototype)[4] 返回对象
<script type="text/javascript"> function Parent(){ this.property="parent_value"; } Parent.prototype={ property:"parent_value_prototype", get:function(){ return this.property; } } function Child(){ this.property="child_value"; } Child.prototype={ property:"child_value_prototype", get:function(){ return this.property; } } Child.prototype=new Parent();//继承 var child=new Child(); console.log(child.get()); //child_value </script>
<script type="text/javascript"> function Parent(){ this.property="parent_value"; } Parent.prototype={ property:"parent_value_prototype", get:function(){ return this.property; } } function Child(){ //this.property="child_value"; } Child.prototype={ property:"child_value_prototype", get:function(){ return this.property; } } Child.prototype=new Parent();//继承 var child=new Child(); console.log(child.get()); //parent_value </script>
<script type="text/javascript"> function Parent(){ //this.property="parent_value"; } Parent.prototype={ property:"parent_value_prototype", get:function(){ return this.property; } } function Child(){ //this.property="child_value"; } Child.prototype={ property:"child_value_prototype", get:function(){ return this.property; } } Child.prototype=new Parent();//继承 var child=new Child(); console.log(child.get()); //parent_value_prototype </script>
<script type="text/javascript"> function Parent(){ //this.property="parent_value"; } Parent.prototype={ //property:"parent_value_prototype", get:function(){ return this.property; } } function Child(){ //this.property="child_value"; } Child.prototype={ property:"child_value_prototype", get:function(){ return this.property; } } Child.prototype=new Parent();//继承 var child=new Child(); console.log(child.get()); //undefined </script>
Resullt:
继承了Parent的Child,
[1] 会从Child的实例中找
[2] 再从Parent实例中找
[3] 再从Parent.prototype中找
( 不会从Child.prototype中找)
(2) 原型链继承 优点+缺点
优点:
父类的实例属性得到了继承
缺点:
[1] 父类实例属性为引用类型, 修改会改变所有的之类
Child继承了Parent后,Child.prototype就成了Parent的一个实例,因此它也拥有name属性
<script type="text/javascript"> function Parent(){ this.name=["yoonA","jessica"]; } function Child(){ } //继承 Child.prototype=new Parent(); //实例child1 var child1=new Child(); //child1修改了值 child1.name.push("krystal"); console.log(child1.name); //["yoonA", "jessica", "krystal"] //实例child2 var child2=new Child(); //但是child2的值也改变了 console.log(child2.name); //["yoonA", "jessica", "krystal"] </script>
[2] 传参问题
在创建子实例时候,不能向父类传参
<script type="text/javascript"> function Parent(name){ this.name=name; } Parent.prototype.say=function(){ console.log(this.name); } function Child(name){ } //继承 Child.prototype=new Parent(); var c=new Child("krystal"); c.say(); //undefined</script>
(3) 类继承
Parent.call(this,name);
构造函数的应用, 子内部call 调用父类的构造器, 实现传参问题
function Parent(name){ this.name=name; } Parent.prototype.say=function(){ console.log(this.name); } function Child(name){ Parent.call(this,name); } //Child.prototype=new Parent(); var c=new Child("krystal"); console.log(c.name); c.say();
优点+缺点:
优点: 可以传参
缺点: 但是Child子 无法使用 Parent父的Prototype的内容
(4) 原型链+类继承
Parent.call(this,name);
传参, 实现对实例属性的继承
Child.prototype=new Parent();
实现原型属性和方法的继承
function Parent(name){ this.name=name; this.school=["hkbu","hku"]; } Parent.prototype.say=function(){ console.log(this.name); } function Child(name){ Parent.call(this,name); // 类继承: 继承实例属性 } Child.prototype=new Parent(); // 原型继承: 继承原型的方法和属性 var c=new Child("krystal"); console.log(c.name); c.say(); c.school.push("NEU"); console.log(c.school); var c2=new Child("jessica"); console.log(c2.school);
Result:
0 0
- [Javascript] 继承 (原型继承, 类继承)
- javascript 类式继承与原型继承
- Javascript原型继承 javascript
- Javascript原型继承 javascript
- JavaScript原型继承小记
- 浅析Javascript原型继承
- 浅析Javascript原型继承
- JavaScript原型 继承
- JavaScript的原型继承
- JavaScript原型继承
- javascript 原型继承
- JavaScript原型和继承
- JavaScript原型和继承
- javascript 原型继承
- JavaScript原型继承机制
- JavaScript 原型 继承
- Javascript原型和继承
- javascript原型链继承
- eclipse优化设置,自动提示篇
- "转义
- 树的最小表示法 poj1635 Subway tree systems
- asp.net取request 集合
- nginx学习3——nginx进程
- [Javascript] 继承 (原型继承, 类继承)
- 这有相当多的优势。
- android局域网通信(三)
- Android 基础代码实例:音频 均衡器 示波器 声场 等
- openGL ES在VS2012中配置遇到的应用程序无法正常启动0xc000007b解决方法
- ArcEngine 专题图关键代码
- const
- phpmyadmin导入导出数据库解决问题(一)
- linux 安装Oracle:包相互依赖解决