typescript編譯小研究

1.繼承的實(shí)現(xiàn)方式

typescript代碼:

class A{
    name: string;

    constructor(name){
        this.name=name;
    }

    print(){
        console.log(this.name);
    }
}

class B extends A{
    old: number;

    constructor(name,old){
        super(name);
        this.old=old;
    }
    
    out(){
        console.log(this.name+this.old);
    }
}

let b=new B('jc',1);
b.out();

用tsc編譯后的ES5代碼:

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 A = (function () {
    function A(name) {
        this.name = name;
    }
    A.prototype.print = function () {
        console.log(this.name);
    };
    return A;
}());
var B = (function (_super) {
    __extends(B, _super);
    function B(name, old) {
        var _this = _super.call(this, name) || this;
        _this.old = old;
        return _this;
    }
    B.prototype.out = function () {
        console.log(this.name + this.old);
    };
    return B;
}(A));
var b = new B('jc', 1);
b.out();
  • 可以看出來typescript中類型之間的繼承是通過寄生組合式繼承來完成的,這里可以給出一個(gè)寄生組合式繼承的簡單例子:
function extend(B,A){
    var prototype=Object.create(A.prototype);
    prototype.construtor=B;
    B.prototype=prototype;
}

function A(name){
    this.name=name;
}

A.prototype.print=function(){
    console.log(this.name);
}

A.prototype.num=1;

function B(name,old){
    A.call(this,name);
    this.old=old;
}

extend(B,A);

B.prototype.ready=function(){
    console.log(this.name+this.old);
}

寄生組合式繼承的基本思路是:不必為了制定子類型的原型來調(diào)用父類型的構(gòu)造函數(shù),只需要將父類型的原型對(duì)象的副本賦給子類型的原型對(duì)象,然后修正一下原型對(duì)象的構(gòu)造函數(shù)指向。

2.私有變量的實(shí)現(xiàn)方式

typescript代碼:

class A{
    private name: string;

    constructor(name){
        this.name=name;
    }

    print(){
        this.console();
    }

    private console(){
        console.log(this.name);
    }
}

let a=new A('jc');
a.print();

tsc編譯后的代碼:

var A = (function () {
    function A(name) {
        this.name = name;
    }
    A.prototype.print = function () {
        this.console();
    };
    A.prototype.console = function () {
        console.log(this.name);
    };
    return A;
}());
var a = new A('jc');
a.print();

可以看到typescript并沒有為私有屬性和函數(shù)做任何處理,但是如果在外部訪問了私有變量,在編譯時(shí)會(huì)報(bào)錯(cuò),可見typescript是在編譯過程的底層實(shí)現(xiàn)了對(duì)私有變量的檢查。

3.靜態(tài)變量的實(shí)現(xiàn)方式

typescript代碼:

class A{
    static num: number=1;
}

console.log(A.num);

tsc編譯后的代碼:

var A = (function () {
    function A() {
    }
    return A;
}());
A.num = 1;
console.log(A.num);

可見靜態(tài)變量時(shí)與類有關(guān)的變量,所以可以直接將該屬性賦給構(gòu)造函數(shù)對(duì)象,讓其成為構(gòu)造函數(shù)的屬性,在類的內(nèi)部也要通過類名才能訪問到靜態(tài)變量。

  1. red
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 TypeScript本質(zhì)上是向JavaScript語言添加了可選的靜態(tài)類型和基于類的面向?qū)ο缶幊?,同時(shí)也支持...
    oWSQo閱讀 8,683評(píng)論 1 45
  • 慕課網(wǎng)@JoJozhai 老師 TypeScript入門課程分享 TypeScript入門 ES5,ES6,JS,...
    shangpudxd閱讀 10,642評(píng)論 0 22
  • { "Unterminated string literal.": "未終止的字符串文本。", "Identifi...
    一粒沙隨風(fēng)飄搖閱讀 11,348評(píng)論 0 3
  • 寫代碼: 1,明確需求。我要做什么? 2,分析思路。我要怎么做?1,2,3。 3,確定步驟。每一個(gè)思路部分用到哪些...
    雨塵1閱讀 323評(píng)論 0 1
  • 繼承 一、混入式繼承 二、原型繼承 利用原型中的成員可以被和其相關(guān)的對(duì)象共享這一特性,可以實(shí)現(xiàn)繼承,這種實(shí)現(xiàn)繼承的...
    magic_pill閱讀 1,128評(píng)論 0 3

友情鏈接更多精彩內(nèi)容