zoukankan      html  css  js  c++  java
  • js 原型链

      ECMAScript中描述了原型链的概念,并将原型链作为实现继承的主要方法。其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。构造函数、原型、实例的关系:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型的对象的内部指针。我们让原型对象等于另一个类型的实例,显然,此时的原型对象将包含一个指向另一个原型的指针,相应地,另一个原型中也包含着一个指向另一个构造函数的指针。假如另一个原型又是另一个类型的实例,上述关系依然成立,如此层层递进,就构成了实例与原型的链条。这就是所谓的原型链的基础概念。

            function SuperType(){
                this.property = true;
            }
            SuperType.prototype.getSuperValue = function(){
                return this.property;
            };
            function SubType(){
                this.subproperty = false;
            }
            //继承了 SuperType
            SubType.prototype = new SuperType();
            SubType.prototype.getSubValue = function(){
                return this.subproperty;
            }
            var instance = new SubType();
            alert(instance.getSuperValue());//true

      由于原型链的关系,我们可以说instance是Object、SuperType或SubType中任何一个类型的实例。同样,只要是原型链中出现的原型,都可以说是该原型链派生的实例的原型。

            alert(instance instanceof Object);//true
            alert(instance instanceof SubType);//true
            alert(instance instanceof SuperType);//true
            alert(Object.prototype.isPrototypeOf(instance));//true
            alert(SuperType.prototype.isPrototypeOf(instance));//true
            alert(SubType.prototype.isPrototypeOf(instance));//true

      原型链该注意的地方:

      1、所有的应用类型默认都继承了Object,而这个继承也是通过原型链实现的。

      2、给原型添加方法的代码一定要放到替换原型的语句之后。

      原型链的问题:

      1、通过原型来实现继承的时候,原型实际会变成另一个类型的实例。于是,原来的实例属性就会变成现在的原型属性。

      2、在创建子类型的实例时,不能向超类型的构造函数中传递参数。实际上,应该说是没有办法在不影响所有对象实例的情况下,给超类的构造函数传递参数。

      在解决原型中包含引用类型值所带来的问题的过程中,有好多种技术:借用构造函数、组合继承、原型式继承、寄生式继承、寄生组合式继承。

      寄生组合式继承普遍被认为是引用类型最理想的继承范式。如下:

            function inheritPrototype(subType,superType){
                //创建对象
                var prototype = Object.create(superType.prototype);
                //增强对象
                prototype.constructor = subType;
                //指定对象
                subType.prototype = prototype;
            }
    
            function SuperType(name){
                this.name = name;
                this.colors = ['red','blue','green'];
            }
            SuperType.prototype.getSuperValue = function(){
                alert(this.name);
            };
            function SubType(name,age){
                SuperType.call(this,name);
                this.age = age;
            }
            inheritPrototype(SubType,SuperType);
            SubType.prototype.sayAge = function(){
                alert(this.age);
            }
    
            var instance = new SubType('zhangsan',23);
            instance.sayAge();//23
            instance.colors.push('black');
            alert(instance.colors);//red,blue,green,black
            var instance2 = new SubType('zhangsan2',24);
            instance2.sayAge();//24
            alert(instance2.colors);//red,blue,green

  • 相关阅读:
    Shell编程------函数应用
    Shell编程------循环语句
    Shell编程------判断语句
    Shell编程------变量、赋值和运算
    Action实现prepareable接口后定义前置方法
    动态代理模式
    hibernate持久化对象,
    view视图总结
    servlet和Struts2的线程安全性对比
    Action获取请求参数的3中方式
  • 原文地址:https://www.cnblogs.com/wangxufeng/p/4613548.html
Copyright © 2011-2022 走看看