• 关于js原型继承


    js的每个类都有一个prototype对象 访问对象的属性时,会先访问到对象自身是否有定义这个属性 如果没有定义,就会去访问对象所属类型的prototype对象是否有此属性

    原型继承就是把类型的prototype指向一个父类的新对象,这样每派生一个新类出来都会构造一个新的父类对象作为原型,这个对象和父类的prototype是分离的 因而可以用于添加子类的新属性而不影响父类

    > function cls(){xx = function(){console.log(11)}}
    undefined
    > cls.prototype.xx = function(){console.log(22)}
    [Function]
    > var x = new cls()
    undefined
    > x.xx()
    22
    undefined
    > x.xx = function(){console.log(33)}
    [Function]
    > x.xx()
    33
    undefined
    > x instanceof cls
    true
    > cls.prototype.xx = function(){console.log(55)}
    [Function]
    > x.xx()
    33
    undefined
    > var y = new cls()
    undefined
    > y.xx()
    55
    undefined
    > cls.prototype.xx = function(){console.log(123)}
    [Function]
    > y.xx()
    123
    undefined
    > cls.prototype
    cls { xx: [Function] }
    > typeof cls.prototype
    'object'
    >
    

      在构造函数中赋值的属性也能被派生类中使用,因为每个对象都运行了一次构造函数,每个对象都有一个此属性的定义 并且这个属性是优先于prototype里的属性的 比如下面的例子:

    > function cls(){this.p=11}
    undefined
    > cls.prototype.p = 22
    22
    > var obj = new cls()
    undefined
    > obj.p
    11
    >

    其实这个和LUA利用元数据表的模拟继承非常相似

    关于原型继承的写法可以参见:

    http://www.cnblogs.com/yangjinjin/archive/2013/02/01/2889368.html

  • 相关阅读:
    webstorm实现手机预览页面
    git 远端版本回退
    Vue开发中的移动端适配(px转换成vw)
    测试web-view,实现小程序和网页之间的切换
    mysql 字符集研究
    Openssl
    Android Activity 去掉标题栏及全屏显示
    Android中的 style 和 theme
    Android sharedPreferences 用法
    Vim 字符集问题
  • 原文地址:https://www.cnblogs.com/fancybit/p/5458851.html
Copyright © 2020-2023  润新知