首页 > web前端 > js教程 > 正文

javascript之面向对象编程之属性继承

巴扎黑
发布: 2016-11-25 11:00:20
原创
1241人浏览过

javascript中,没有继承关键字: extends。那么,它是通过哪些方法,在用构造函数生成对象时,把对象基于另外一个构造函数,进行属性的生成(继承/拷贝)的呢? 即:对一个函数使用 new 关键字生成对象时,其对象的属性,可以来自于其它函数。 

本文提供两种写法: 

第一种(非正式): 
但需要理解这种用法。 

Javascript代码  

function Animal (name, age){  

   this.name = name;  

   this.age = age;  

立即学习Java免费学习笔记(深入)”;

}  

  

function Dog (name, age){  

   this.i = Animal;  

   this.i(name, age);  

}  

  

d = new Dog('hello',20);  

  

console.log(d);  

  

/* 

 

Dog {name: "hello", age: 20} 

    age: 20 

    i: function Animal(name, age) 

    name: "hello" 

    __proto__:  

        constructor: function Dog(name, age) 

 

 

*/  


上面一种写法是引用外部函数 Animal 作为自己的一个内部成员函数。 
这是一种匿名函数的写法。 

相当于: 

Javascript代码  

function Dog (name, age){  

   this.i = function Animal (name, age){  

       this.name = name;  

       this.age = age;  

   }  

   this.i(name, age);  

}  



相当于: 

Javascript代码  

function Dog (name, age){  

   /* 

   When calling a function, instead of using the 'new' keyword to create 

   object,  

 

              // Calling a function  

              Animal(); 

 

              //Using the 'new' keyword to create object. 

              new Animal(); 

 

 

   The inner 'this' is the same one of the outer 'this'. Because  

豆包AI编程
豆包AI编程

豆包推出的AI编程助手

豆包AI编程 483
查看详情 豆包AI编程

   there is no 'this' object created inside of the function, so it has to  

   refer to the outer one. 

   */  

     

   this.i = function (name, age){  

       this.name = name;           // 2. so the inner "this" is the same   

       this.age = age;             // one of the outer "this".  

   }  

   this.i(name, age);              // 1. function call, no 'this' created.  

  

}                                    



思考:调用函数时,"this"是谁?? 
既然,函数调用不生成 "this" 对象。 
那么直接在 Dog 内调用 Animal 不可以吗? 
答案:否 

Java代码  

/* 

  Which 'this' the called function belongs to, the inner 'this' inside of 

  the called function refers to that 'this' object. 

*/  

  

function Dog (name, age){ // if call Animal directly,  

    Animal(name,age);     // the Animal function here belongs to 'window',  

}                         // so 'this' inside of Animal refers to 'window'  





第二种(正式): 
使用 apply() 函数 或 call() 函数 

apply() : apply "this" object to the(that) function, then call it. 

Javascript代码  

function Animal (name, age){  

   this.name = name;  

   this.age = age;  

立即学习Java免费学习笔记(深入)”;

}  

  

function Dog (name, age){  

   Animal.apply(this, arguments);  // apply "this" object to Animal function.    

                                   // or    

                                   // call Animal function with a given 'this'   

                                   // object, instead of referring to other 'this'.  

}  

  

d = new Dog('hello',20);  

  

console.log(d);  

  

/* 

 

Dog {name: "hello", age: 20} 

    age: 20 

    name: "hello" 

    __proto__:  

        constructor: function Dog(name, age) 

 

 

*/  

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号