Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 键入此范围的脚本don';不要在封闭的环境中工作_Typescript_Scope_This - Fatal编程技术网

Typescript 键入此范围的脚本don';不要在封闭的环境中工作

Typescript 键入此范围的脚本don';不要在封闭的环境中工作,typescript,scope,this,Typescript,Scope,This,我是打字脚本的初学者。我原以为typescript可以用JavaScript解决我在这个范围内的所有问题,但事实并非如此。 请参见下面的示例代码: 等级车{ 私有名称:字符串; 私人速度:数字; 构造函数(名称:string){ this.name=名称; 这个速度=0; } 专用加速比(递增者:数字){ 这个。速度+=增量; } 公共加速器(电源:数字){ console.log(this);//这是一个Car对象 设置超时( 函数(){ //这是一个超时对象-WTF!!! //这不是打字稿

我是打字脚本的初学者。我原以为typescript可以用JavaScript解决我在这个范围内的所有问题,但事实并非如此。 请参见下面的示例代码:

等级车{
私有名称:字符串;
私人速度:数字;
构造函数(名称:string){
this.name=名称;
这个速度=0;
}
专用加速比(递增者:数字){
这个。速度+=增量;
}
公共加速器(电源:数字){
console.log(this);//这是一个Car对象
设置超时(
函数(){
//这是一个超时对象-WTF!!!
//这不是打字稿吗?
//我的“this”对象范围是什么?
console.log(this);
this.speedUp(power*1.34);//类型错误:this.speedUp不是函数
},    
1000
);
}
public getVelocity():编号{
返回此值。速度;
}
}
让我的车=新车(“梅赛德斯”);
按下加速器(2);

console.log(myCar.getVelocity())我不知道它是否优雅,但它可以:

public pressAccelerator (power: number){
    console.log(this); //here this is a Car object   
    let self = this;
    setTimeout(
        function(){
            console.log(self);
            self.speedUp(power*1.34);
        },    
        1000
    );
}

您建议使用箭头符号:

class Car {
    private name : string;
    private velocity: number;

    constructor (name: string) {
        this.name = name;
        this.velocity = 0;
    }

    private speedUp = (incrementer: number): void => {
        console.log(this);
        this.velocity += incrementer;
    } 

    public pressAccelerator = (power: number): void => {

        console.log(this); //here this is a Car object       
        setTimeout(() => this.speedUp(power*1.34), 1000);
    }
    public getVelocity = (): number => {
        return this.velocity;
    }
}

let myCar = new Car("Mercedes");
myCar.pressAccelerator(2);
console.log(myCar.getVelocity());

将汇编为:

var Car = /** @class */ (function () {
    function Car(name) {
        var _this = this;
        this.speedUp = function (incrementer) {
            console.log(_this);
            _this.velocity += incrementer;
        };
        this.pressAccelerator = function (power) {
            console.log(_this); //here this is a Car object       
            setTimeout(function () { return _this.speedUp(power * 1.34); }, 1000);
        };
        this.getVelocity = function () {
            return _this.velocity;
        };
        this.name = name;
        this.velocity = 0;
    }
    return Car;
}());
var myCar = new Car("Mercedes");
myCar.pressAccelerator(2);
console.log(myCar.getVelocity());
如您所见,typescript将修复该闭包。你可以读更多


它无法解决的一个问题是,如果您在计时器回调中定义了一个作用域函数:(非箭头函数)


使用箭头功能,该功能保留此

public pressAccelerator (power: number){
    console.log(this); //here this is a Car object       
    setTimeout(
        () => {
            console.log(this);
            this.speedUp(power*1.34);
        },    
        1000
    );
}

匿名函数中的此
指函数本身。正如在一些答案中所说,使用箭头函数有一种不同的行为,与您想要的匹配。
public pressAccelerator (power: number){
    console.log(this); //here this is a Car object       
    setTimeout(
        () => {
            console.log(this);
            this.speedUp(power*1.34);
        },    
        1000
    );
}