zoukankan      html  css  js  c++  java
  • 关于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

  • 相关阅读:
    DPDK — 网卡初始化流程(Intel 82599 ixgbe 网卡驱动示例)
    leetcode 3. 无重复字符的最长子串
    20193120 实验四 Python综合实践
    hadoop常用的端口号
    Django学习笔记
    ORACLE EBS AP invoice 到付款的数据流
    EBS 系统标准职责定义MAP
    Advanced Pricing
    Oracle Advanced Pricing White Papers
    增加AP INVOICE 行&分配行
  • 原文地址:https://www.cnblogs.com/fancybit/p/5458851.html
Copyright © 2011-2022 走看看