Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel 会将箭头函数编译成什么? #46

Open
hanyueqiang opened this issue Apr 9, 2021 · 0 comments
Open

Babel 会将箭头函数编译成什么? #46

hanyueqiang opened this issue Apr 9, 2021 · 0 comments

Comments

@hanyueqiang
Copy link
Owner

babel会直接将箭头函数编译为普通函数,用到到this的地方会在箭头函数定义的作用域头部将this赋值给_this

编译箭头函数babel

@babel/plugin-transform-arrow-functions

示例

var a = (b) => b;

const double = [1,2,3].map((num) => num * 2);

var bob = {
  _name: "Bob",
  _friends: ["Sally", "Tom"],
  printFriends() {
    this._friends.forEach(f =>
      console.log(this._name + " knows " + f));
  }
};

编译后

var a = function (b) {
  return b;
};

const double = [1, 2, 3].map(function (num) {
  return num * 2;
});

var bob = {
  _name: "Bob",
  _friends: ["Sally", "Tom"],
  printFriends() {
    var _this = this;

    this._friends.forEach(function (f) {
      return console.log(_this._name + " knows " + f);
    });
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant