zoukankan      html  css  js  c++  java
  • 【javascript 】组合式继承

    开发人员采用的js的继承结构

    function inheritPrototype(subType, superType) {
        var prototype = object(superType.prototype);
        prototype.constructor = subType;
        subType.prototype = prototype;
    }
    
    function obj() {
        function F() {}
        F.prototype = o;
        return new F();
    }
    function create(parent, test) {
        var f = obj(parent.prototype); //创建对象
        f.constructor = test; //增强对象
    }
    function Parent(name) {
        this.name = name;
        this.arr = ['brother', 'sister', 'parents'];
    }
    
    Parent.prototype.run = function() {
        return this.name;
    };
    
    function Child(name, age) {
        Parent.call(this, name);
        this.age = age;
    }
    inheritPrototype(Parent, Child); //通过这里实现继承
    var test = new Child('peter', 20);
    test.arr.push('new');
    console.log(test.arr); //brother,sister,parents,new
    console.log(test.run()); //只共享了方法
    var test2 = new Child('jack', 22);
    console.log(test2.arr); //引用问题解决
    
    
    关于call apply
    call(thisObj, Object); // call接收一个对象
    apply(thisObj, [argArray]) //apply接收一个数组
    call和apply可以用来改变函数中this的指向: 全选复制放进笔记 // 定义一个全局函数
    function f() {
        console.log(this.name);
    }
    // 定义一个全局变量
    var name = 'peter';
    var obj = {
        name: 'jack';
    };
    f.apply(window); //apple, 此时this 等于window  相当于window.f()
    f.apply(obj);
  • 相关阅读:
    json编解码
    Grok 正则捕获
    logstash date插件介绍
    logstash 字段类型转换后 需要刷新
    logstash 防止实际处理时间跟事件产生时间略有偏差
    导入旧数据需要 使用date插件
    nginx和tomcat的响应时间
    解决kibana 4 关于响应时间的问题
    go 可以开发桌面应用
    windows下go语言环境
  • 原文地址:https://www.cnblogs.com/airven/p/5350245.html
Copyright © 2011-2022 走看看