zoukankan      html  css  js  c++  java
  • 从Typescript看原型链

    话不多说先来段代码

    class Parent {
        private name:string;
        constructor(name) {
            this.name = name;
        }
    
        public getName():string {
            return this.name;
        }
    
        public setName(name:string):void {
            this.name = name;
        }
    
        sayHi() {
            return `Parent name is ${this.name}`;
        }
    }
    
    class Child extends Parent {
        constructor(name) {
            super(name);
        }
    
        sayHi() {
            return `Child call Parent' sayHi() to show parent name is ${super.getName()}`;
        }
    }
    
    const xiaoxu = new Child('xiaoxu');
    console.log(xiaoxu.sayHi());
    
    
    var __extends = (this && this.__extends) || (function() {
        var extendStatics = Object.setPrototypeOf || ({
            __proto__: []
        } instanceof Array && function(d, b) {
            d.__proto__ = b;
        }
        ) || function(d, b) {
            for (var p in b)
                if (b.hasOwnProperty(p))
                    d[p] = b[p];
        }
        ;
        return function(d, b) {
            extendStatics(d, b);
            function __() {
                this.constructor = d;
            }
            d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype,
            new __());
        }
        ;
    }
    )();
    var Parent = /** @class */
    (function() {
        function Parent(name) {
            this.name = name;
        }
        Parent.prototype.getName = function() {
            return this.name;
        }
        ;
        Parent.prototype.setName = function(name) {
            this.name = name;
        }
        ;
        Parent.prototype.sayHi = function() {
            return "Parent name is " + this.name;
        }
        ;
        return Parent;
    }());
    var Child = /** @class */
    (function(_super) {
        __extends(Child, _super);
        function Child(name) {
            return _super.call(this, name) || this;
        }
        Child.prototype.sayHi = function() {
            return "Child call Parent' sayHi() to show parent name is " + _super.prototype.getName.call(this);
        }
        ;
        return Child;
    }(Parent));
    var xiaoxu = new Child('xiaoxu');
    console.log(xiaoxu.sayHi());
    
    
    var __extends = this && this.__extends;
    var extendsFn = function() {
        var extenStatics = Object.setPrototypeOf;
        
    }
    __extends = __extends || 
    
    // 这句其实比较有意思
    {__proto__: []} instanceof Array && function ss(){}
    
    // 首先我们知道如果var a = new A(), 那么a.__proto__ = A.prototype 而A.prototype.__proto__ = Object.prototype
    //所以假设var a = {__proto__: []},那么也就意味着a.__proto__ = []
    // 再说下instanceof,我们先写一个伪instanceof
    // 所以[].__proto__ = Array.prototype 当然所以可以理解为a是[]的子类,是Array的子子类。
    // 其实这段话的意思就是来判断当前环境是否支持__proto__作为隐式链接来进行原型链查找,其实是一种环境检测的方法,非常赞。
    
    // 这样d.__proto = b,使得d可以引用b上的属性,即静态变量
    
    // A instanceof B  ===  fakeInstanceof(A, B)
    function fakseInstanceof(a, b) {
        var L = a.__proto__
        var M = b.prototype
        if(L === M) {
            return true;
        } else {
            return false;
        }
    }
    
    // 更帅的一段来了
    function __() {
        this.constructor = d;
    }
    
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, d = new __())
    
    // 傻眼了吧,这里__就是一个桥梁,因为d = new __()所以d.__proto__ = __.prototype, 又因为__.prototype = b.prototype, 
    // 这样我们就可以在d和b之间形成原型链,且将隔离性做到很好
    // 别忘记d = new __()会改变constructor,这里我们用this.constructor很好的结局了。
    // ??这里是否有效率提升,参考anujs的解释。
    
  • 相关阅读:
    Web服务器安全设置
    java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
    Java IO和Java NIO在文件拷贝上的性能差异分析
    Java高效读取大文件
    NIO入门之轻松读取大文件
    我来说说java的NIO
    java读取大文件 超大文件的几种方法
    @RequestBody 的正确使用办法
    友鱼项目知识点
    怎样查看Tomcat动态控制台信息
  • 原文地址:https://www.cnblogs.com/shineyao/p/7847075.html
Copyright © 2011-2022 走看看