美文网首页我爱编程
TypeScript中的this指向问题

TypeScript中的this指向问题

作者: PaulLuv | 来源:发表于2018-03-11 00:25 被阅读56次

TypeScript 是 JavaScript 的一个超集,主要提供了类型系统对 ES6 的支持,它由 Microsoft 开发,代码开源于 GitHub 上。

TypeScript 是 JavaScript 的类型的超集,它可以编译成纯 JavaScript。编译出来的 JavaScript 可以运行在任何浏览器上。TypeScript 编译工具可以运行在任何服务器和任何系统上。TypeScript 是开源的。

明确上面一点后,我们再来看TypeScript中的this指向问题。

JavaScript 中的this指向

下面通过例子来探讨一下TypeScript中的this指向问题:

class demo{
    private str:string = "hello";
    public abc(){
        console.log( this.str );
    }
}

编译后的js文件代码如下:

var demo = /** @class */ (function () {
    function demo() {
        this.str = "hello";
    }
    demo.prototype.abc = function () {
        console.log(this.str);
    };
    return demo;
}());

这样生成的JS文件也并不难理解。我们再简单修改一下JS代码,

var demo = (function () {
    function demo() {
        this.str = "hello";
    }
    demo.prototype.abc = function () {
        console.log(this.str);
    };
    return demo;
})();

var d = new demo();
d.abc();

这样运行后,可以看到打印“hello”字符。
再修改一下:

var demo = (function () {
    function demo() {
        this.str = "hello";
    }
    demo.prototype.abc = function () {
        console.log( this.str);
    };
    demo.prototype.doing = function(callback){
        callback();
    }
    return demo;
})();
var d = new demo();
d.abc();
d.doing(d.abc);

此时运行就会先输出一个hello在输出一个undefined
这就是JS中的比较令人困惑的作用域问题了。
为避免这种问题,我们需要做如下修改

var demo = (function () {
    var that;
    function demo() {
        this.str = "hello";
        that = this;
    }
    demo.prototype.abc = function () {
        console.log( that.str);
    };
    demo.prototype.doing = function(callback){
        callback();
    }
    return demo;
})();
var d = new demo();
d.abc();
d.doing(d.abc);

TypeScript为了避免这个问题,提供了变通的解决方案:

class demo{
    private str:string = "hello";
    public abc(){
        console.log( this.str );
    }
    public doing(callback){
        callback();
    }
    public start(){
        this.doing(this.abc);
    }
}

上面这个代码,编译后的JS代码如下:

var demo = /** @class */ (function () {
    function demo() {
        this.str = "hello";
    }
    demo.prototype.abc = function () {
        console.log(this.str);
    };
    demo.prototype.doing = function (callback) {
        callback();
    };
    demo.prototype.start = function () {
        this.doing(this.abc);
    };
    return demo;
}());

如果我们换一种写法

class demo{
    private str:string = "hello";
    public abc(){
        console.log( this.str );
    }
    public doing(callback){
        callback();
    }
    public start(){
        this.doing(()=> this.abc());
    }
}

这时候编译后的JS代码如下:

var demo = /** @class */ (function () {
    function demo() {
        this.str = "hello";
    }
    demo.prototype.abc = function () {
        console.log(this.str);
    };
    demo.prototype.doing = function (callback) {
        callback();
    };
    demo.prototype.start = function () {
        var _this = this;
        this.doing(function () { return _this.abc(); });
    };
    return demo;
}());

TypeScript是将这种作用域的问题封装到了语言层面,并为其取名“Arrow Function”。

相关文章

  • TypeScript中的this指向问题

    TypeScript 是 JavaScript 的一个超集,主要提供了类型系统和对 ES6 的支持,它由 Micr...

  • JavaScript中This指向问题

    1 全局作用域或者普通函数的this指向Windows //直接打印console.log(this) //win...

  • js中this指向问题

    this的指向在函数定义的时候是无法确定的,只有函数执行的时候才能确定this到底指向谁,实际this指向是调用他...

  • react中this指向问题

    一.不对this指向做任何改变 class Btn extends React.Component{ rend...

  • javascript中this指向问题

    正确理解了this,才算是迈入了JavaScript这门语言JavaScript特性 由于在运行时绑定的特性,js...

  • JavaScript中this指向问题

    直接调用 即为通过函数名()调用,这是,函数内部的this指向全局变量,在浏览器中全局对象是window,在nod...

  • JS中this指向问题

    首先声明,添加删除线的都是不太确定的 下面我们分情况解释: 1、函数调用模式--当一个函数并非一个对象的属性时,那...

  • React 中 this指向问题

    在写react mobx的demo时,给checkbox 添加一个onChange事件,并且忘记在construc...

  • js中this指向问题?

    This是一个关键字,它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。 this 是在函数被调用时确...

  • vue中this指向问题

    根据VUE官方文档给出的解释 在Vue所有的生命周期钩子方法(如created,mounted,updated以及...

网友评论

    本文标题:TypeScript中的this指向问题

    本文链接:https://www.haomeiwen.com/subject/hrtbfftx.html