gpt4 book ai didi

javascript - 意外字符 = 当使用带有 webpack/babel env 预设的箭头函数时

转载 作者:行者123 更新时间:2023-12-03 02:44:08 26 4
gpt4 key购买 nike

这个语法有什么问题:

export default class Pano {
constructor() {
this.buildReferences();
}

buildReferences=()=> {
console.log(window);
}
}

它抛出错误 Parsing error: Unexpected token = .

我在其他项目中使用过它,所以我不确定发生了什么。

最佳答案

A class body can only contain methods, but not data properties

因此,当您在 ES6 类中时,buildReferences() 语法的工作方式类似于箭头函数语法。

如果你想实际做你想做的事情,你必须在另一个函数中完成它,比如构造函数:

class Pano {
constructor() {

// you can create a data property here and assign it a function
this.buildReferences = ()=> {
console.log("hello");
}

this.buildReferences();

}

// this is the syntax for a function in an ES6 class
regularFunction(){

}

}

let test = new Pano();

仅供引用:TypeScript 支持数据属性,因此您所做的事情可以在 Typescript 中工作。

关于javascript - 意外字符 = 当使用带有 webpack/babel env 预设的箭头函数时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48175713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com