再谈javascript原型继承


Posted in Javascript onNovember 10, 2014

真正意义上来说Javascript并不是一门面向对象的语言,没有提供传统的继承方式,但是它提供了一种原型继承的方式,利用自身提供的原型属性来实现继承。

原型与原型链

说原型继承之前还是要先说说原型和原型链,毕竟这是实现原型继承的基础。
在Javascript中,每个函数都有一个原型属性prototype指向自身的原型,而由这个函数创建的对象也有一个__proto__属性指向这个原型,而函数的原型是一个对象,所以这个对象也会有一个__proto__指向自己的原型,这样逐层深入直到Object对象的原型,这样就形成了原型链。下面这张图很好的解释了Javascript中的原型和原型链的关系。

再谈javascript原型继承

每个函数都是Function函数创建的对象,所以每个函数也有一个__proto__属性指向Function函数的原型。这里需要指出的是,真正形成原型链的是每个对象的__proto__属性,而不是函数的prototype属性,这是很重要的。

原型继承

基本模式

var Parent = function(){

    this.name = 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(){

    this.name = 'child' ;

} ;

Child.prototype = new Parent() ;
var parent = new Parent() ;

var child = new Child() ;
console.log(parent.getName()) ; //parent

console.log(child.getName()) ; //child

这种是最简单实现原型继承的方法,直接把父类的对象赋值给子类构造函数的原型,这样子类的对象就可以访问到父类以及父类构造函数的prototype中的属性。 这种方法的原型继承图如下:

再谈javascript原型继承

这种方法的优点很明显,实现十分简单,不需要任何特殊的操作;同时缺点也很明显,如果子类需要做跟父类构造函数中相同的初始化动作,那么就得在子类构造函数中再重复一遍父类中的操作:

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(name){

    this.name = name || 'child' ;

} ;

Child.prototype = new Parent() ;
var parent = new Parent('myParent') ;

var child = new Child('myChild') ;
console.log(parent.getName()) ; //myParent

console.log(child.getName()) ; //myChild

上面这种情况还只是需要初始化name属性,如果初始化工作不断增加,这种方式是很不方便的。因此就有了下面一种改进的方式。

借用构造函数

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(name){

    Parent.apply(this,arguments) ;

} ;

Child.prototype = new Parent() ;
var parent = new Parent('myParent') ;

var child = new Child('myChild') ;
console.log(parent.getName()) ; //myParent

console.log(child.getName()) ; //myChild

上面这种方法在子类构造函数中通过apply调用父类的构造函数来进行相同的初始化工作,这样不管父类中做了多少初始化工作,子类也可以执行同样的初始化工作。但是上面这种实现还存在一个问题,父类构造函数被执行了两次,一次是在子类构造函数中,一次在赋值子类原型时,这是很多余的,所以我们还需要做一个改进:

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(name){

    Parent.apply(this,arguments) ;

} ;

Child.prototype = Parent.prototype ;
var parent = new Parent('myParent') ;

var child = new Child('myChild') ;
console.log(parent.getName()) ; //myParent

console.log(child.getName()) ; //myChild

这样我们就只需要在子类构造函数中执行一次父类的构造函数,同时又可以继承父类原型中的属性,这也比较符合原型的初衷,就是把需要复用的内容放在原型中,我们也只是继承了原型中可复用的内容。上面这种方式的原型图如下:

再谈javascript原型继承

临时构造函数模式(圣杯模式)

上面借用构造函数模式最后改进的版本还是存在问题,它把父类的原型直接赋值给子类的原型,这就会造成一个问题,就是如果对子类的原型做了修改,那么这个修改同时也会影响到父类的原型,进而影响父类对象,这个肯定不是大家所希望看到的。为了解决这个问题就有了临时构造函数模式。

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(name){

    Parent.apply(this,arguments) ;

} ;

var F = new Function(){} ;

F.prototype = Parent.prototype ;

Child.prototype = new F() ;
var parent = new Parent('myParent') ;

var child = new Child('myChild') ;
console.log(parent.getName()) ; //myParent

console.log(child.getName()) ; //myChild

该方法的原型继承图如下:

再谈javascript原型继承

很容易可以看出,通过在父类原型和子类原型之间加入一个临时的构造函数F,切断了子类原型和父类原型之间的联系,这样当子类原型做修改时就不会影响到父类原型。

我的方法

《Javascript模式》中到圣杯模式就结束了,可是不管上面哪一种方法都有一个不容易被发现的问题。大家可以看到我在'Parent'的prototype属性中加入了一个obj对象字面量属性,但是一直都没有用。我们在圣杯模式的基础上来看看下面这种情况:

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(name){

    Parent.apply(this,arguments) ;

} ;

var F = new Function(){} ;

F.prototype = Parent.prototype ;

Child.prototype = new F() ;
var parent = new Parent('myParent') ;

var child = new Child('myChild') ;
console.log(child.obj.a) ; //1

console.log(parent.obj.a) ; //1

child.obj.a = 2 ;

console.log(child.obj.a) ; //2

console.log(parent.obj.a) ; //2

在上面这种情况中,当我修改child对象obj.a的时候,同时父类的原型中的obj.a也会被修改,这就发生了和共享原型同样的问题。出现这个情况是因为当访问child.obj.a的时候,我们会沿着原型链一直找到父类的prototype中,然后找到了obj属性,然后对obj.a进行修改。再看看下面这种情况:

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : 1} ;
var Child = function(name){

    Parent.apply(this,arguments) ;

} ;

var F = new Function(){} ;

F.prototype = Parent.prototype ;

Child.prototype = new F() ;
var parent = new Parent('myParent') ;

var child = new Child('myChild') ;
console.log(child.obj.a) ; //1

console.log(parent.obj.a) ; //1

child.obj.a = 2 ;

console.log(child.obj.a) ; //2

console.log(parent.obj.a) ; //2

这里有一个关键的问题,当对象访问原型中的属性时,原型中的属性对于对象来说是只读的,也就是说child对象可以读取obj对象,但是无法修改原型中obj对象引用,所以当child修改obj的时候并不会对原型中的obj产生影响,它只是在自身对象添加了一个obj属性,覆盖了父类原型中的obj属性。而当child对象修改obj.a时,它先读取了原型中obj的引用,这时候child.obj和Parent.prototype.obj是指向同一个对象的,所以child对obj.a的修改会影响到Parent.prototype.obj.a的值,进而影响父类的对象。AngularJS中关于$scope嵌套的继承方式就是模范Javasript中的原型继承来实现的。
根据上面的描述,只要子类对象中访问到的原型跟父类原型是同一个对象,那么就会出现上面这种情况,所以我们可以对父类原型进行拷贝然后再赋值给子类原型,这样当子类修改原型中的属性时就只是修改父类原型的一个拷贝,并不会影响到父类原型。具体实现如下:

var deepClone = function(source,target){

    source = source || {} ;

    var toStr = Object.prototype.toString ,

        arrStr = '[object array]' ;

    for(var i in source){

        if(source.hasOwnProperty(i)){

            var item = source[i] ;

            if(typeof item === 'object'){

                target[i] = (toStr.apply(item).toLowerCase() === arrStr) : [] ? {} ;

                deepClone(item,target[i]) ;    

            }else{

                deepClone(item,target[i]) ;

            }

        }

    }

    return target ;

} ;

var Parent = function(name){

    this.name = name || 'parent' ;

} ;

Parent.prototype.getName = function(){

    return this.name ;

} ;

Parent.prototype.obj = {a : '1'} ;
var Child = function(name){

    Parent.apply(this,arguments) ;

} ;

Child.prototype = deepClone(Parent.prototype) ;
var child = new Child('child') ;

var parent = new Parent('parent') ;
console.log(child.obj.a) ; //1

console.log(parent.obj.a) ; //1

child.obj.a = '2' ;

console.log(child.obj.a) ; //2

console.log(parent.obj.a) ; //1

综合上面所有的考虑,Javascript继承的具体实现如下,这里只考虑了Child和Parent都是函数的情况下:
var deepClone = function(source,target){

    source = source || {} ;

    var toStr = Object.prototype.toString ,

        arrStr = '[object array]' ;

    for(var i in source){

        if(source.hasOwnProperty(i)){

            var item = source[i] ;

            if(typeof item === 'object'){

                target[i] = (toStr.apply(item).toLowerCase() === arrStr) : [] ? {} ;

                deepClone(item,target[i]) ;    

            }else{

                deepClone(item,target[i]) ;

            }

        }

    }

    return target ;

} ;
var extend = function(Parent,Child){

    Child = Child || function(){} ;

    if(Parent === undefined)

        return Child ;

    //借用父类构造函数

    Child = function(){

        Parent.apply(this,argument) ;

    } ;

    //通过深拷贝继承父类原型    

    Child.prototype = deepClone(Parent.prototype) ;

    //重置constructor属性

    Child.prototype.constructor = Child ;

} ;

总结

说了这么多,其实Javascript中实现继承是十分灵活多样的,并没有一种最好的方法,需要根据不同的需求实现不同方式的继承,最重要的是要理解Javascript中实现继承的原理,也就是原型和原型链的问题,只要理解了这些,自己实现继承就可以游刃有余。

Javascript 相关文章推荐
Firefox中autocomplete="off" 设置不起作用Bug的解决方法
Mar 25 Javascript
javascript 判断字符串是否包含某字符串及indexOf使用示例
Oct 18 Javascript
js中的getAttribute方法使用示例
Aug 01 Javascript
JavaScript获取键盘按键的键码(参照表)
Jan 10 Javascript
Ionic项目中Native Camera的使用方法
Jun 07 Javascript
JavaScript常用数学函数用法示例
May 14 Javascript
webpack里使用jquery.mCustomScrollbar插件的方法
May 30 jQuery
vue中如何让子组件修改父组件数据
Jun 14 Javascript
vue实现绑定事件的方法实例代码详解
Jun 20 Javascript
vue-router的钩子函数用法实例分析
Oct 26 Javascript
Vue中常用rules校验规则(实例代码)
Nov 14 Javascript
vue 组件之间事件触发($emit)与event Bus($on)的用法说明
Jul 28 Javascript
让angularjs支持浏览器自动填表
Nov 10 #Javascript
使用cluster 将自己的Node服务器扩展为多线程服务器
Nov 10 #Javascript
前端必备神器 Snap.svg 弹动效果
Nov 10 #Javascript
浅谈JavaScript 框架分类
Nov 10 #Javascript
使用script的src实现跨域和类似ajax效果
Nov 10 #Javascript
jquery插件推荐 jquery.cookie
Nov 09 #Javascript
jquery插件推荐浏览器嗅探userAgent
Nov 09 #Javascript
You might like
php 中文处理函数集合
2008/08/27 PHP
PHP中strtotime函数使用方法详解
2011/11/27 PHP
PHP设计模式之装饰者模式
2012/02/29 PHP
php class中public,private,protected的区别以及实例分析
2013/06/18 PHP
基于PHPexecl类生成复杂的报表表头示例
2016/10/14 PHP
php实现留言板功能(会话控制)
2017/05/23 PHP
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
2020/03/26 PHP
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
js中split函数的使用方法说明
2013/12/26 Javascript
JavaScript实现鼠标滑过图片变换效果的方法
2015/04/16 Javascript
javascript实现在网页任意处点左键弹出隐藏菜单的方法
2015/05/13 Javascript
老生常谈JQuery data方法的使用
2016/09/09 Javascript
详解javascript事件绑定使用方法
2016/10/20 Javascript
浅谈MVC+EF easyui dataGrid 动态加载分页表格
2016/11/10 Javascript
Bootstrap基本插件学习笔记之轮播幻灯片(23)
2016/12/08 Javascript
jQuery+ajax的资源回收处理机制分析
2017/01/07 Javascript
vue 解决异步数据更新问题
2019/10/29 Javascript
JS window对象简单操作完整示例
2020/01/14 Javascript
从零开始在vue-cli4配置自适应vw布局的实现
2020/06/08 Javascript
vue实现从外部修改组件内部的变量的值
2020/07/30 Javascript
python列出目录下指定文件与子目录的方法
2015/07/03 Python
python采用django框架实现支付宝即时到帐接口
2016/05/17 Python
详解 Python 读写XML文件的实例
2017/08/02 Python
TensorFlow saver指定变量的存取
2018/03/10 Python
python实现将一个数组逆序输出的方法
2018/06/25 Python
Python3 itchat实现微信定时发送群消息的实例代码
2019/07/12 Python
Python读取实时数据流示例
2019/12/02 Python
Python While循环语句实例演示及原理解析
2020/01/03 Python
pandas分批读取大数据集教程
2020/06/06 Python
Python实现爬取网页中动态加载的数据
2020/08/17 Python
利用python为PostgreSQL的表自动添加分区
2021/01/18 Python
个人查摆剖析材料
2014/02/04 职场文书
给校长的建议书100字
2014/05/16 职场文书
大学生求职简历自我评价
2015/03/02 职场文书
单位综合评价意见
2015/06/05 职场文书
工作简报范文
2015/07/21 职场文书