zoukankan      html  css  js  c++  java
  • js继承模式

    组合继承是js常用的继承模式,指的是将原型链和借用构造函数的技术结合在一起。其中的思想是使用原型链实现原型属性和方法的继承,

    而通过借用构造函数实现对属性的继承。

    例子:

    <script>
            function SuperType(name){
                this.name = name;
                this.colors = ["red","blue"];
            }
            SuperType.prototype.sayName = function(){
                alert(this.name);
            }
            function SubType(name,age){
                //继承属性
                SuperType.call(this,name);
                this.age = age;
            }
            //继承方法
            SubType.prototype = new SuperType();
            SubType.prototype.constructor = SubType;
            //添加方法
            SubType.prototype.sayAge = function(){
                alert(this.age);
            }
            var instance1 = new SubType("jie",23);
            instance1.colors.push("green");
            alert(instance1.colors);
            instance1.sayName();
            instance1.sayAge();
    
            var instance2 = new SubType("fei",23);
            alert(instance2.colors);
            instance2.sayName();
            instance2.sayAge();
    
        </script>
  • 相关阅读:
    Explain执行计划
    SQL优化(SQL + 索引)
    SQL(含索引)
    分组聚合 merger
    服务分组 group
    多注册中心 registry
    多协议 protocol
    常用协议 —— webservice://
    常用协议 —— http://
    最强AngularJS资源合集
  • 原文地址:https://www.cnblogs.com/scnuwangjie/p/4918608.html
Copyright © 2011-2022 走看看