相关疑难解决方法(0)

Typescript中的类方法与属性函数与属性箭头函数之间有什么区别?

我想知道-类方法,作为函数的类属性和作为箭头函数的类属性之间有什么区别?在不同的方法变体中,“ this”关键字的行为是否有所不同?

class Greeter {

  constructor() {
    this.greet();
    this.greet2();
    this.greet3();
  }

  greet() {
    console.log('greet1', this);
  }

  greet2 = () => {
    console.log('greet2', this);
  }

  greet3 = function() {
    console.log('greet3', this);
  }
}


let bla = new Greeter();
Run Code Online (Sandbox Code Playgroud)

编辑:从编译的打字稿的javascript输出:

var Greeter = /** @class */ (function () {
function Greeter() {
    var _this = this;
    this.greet2 = function () {
        console.log('greet2', _this);
    };
    this.greet3 = function () {
        console.log('greet3', this);
    };
    this.greet();
    this.greet2();
    this.greet3();
}
Greeter.prototype.greet = function () {
    console.log('greet1', …
Run Code Online (Sandbox Code Playgroud)

javascript oop typescript

7
推荐指数
2
解决办法
441
查看次数

标签 统计

javascript ×1

oop ×1

typescript ×1