Javascript 语法错误:编译时js文件中出现意外标记
错误代码:Javascript 语法错误:编译时js文件中出现意外标记,javascript,compiler-errors,Javascript,Compiler Errors,错误代码: class App extends Component { let memos = { date: new Date(), text: 'I hope you enjoy learning React!' } render() { return ( <tr> <td>You have no plans yet </t
class App extends Component {
let memos = {
date: new Date(),
text: 'I hope you enjoy learning React!'
}
render() {
return (
<tr>
<td>You have no plans yet </td>
</tr>
);
}
}
我刚开始学习反应,我发现了一个错误。 具体是什么?代码:
let memos = {
date: new Date(),
text: 'I hope you enjoy learning React!'
}
应该包装在构造函数(或其他方法)中,它不能挂在类本身中:
constructor() {
let memos = {
date: new Date(),
text: 'I hope you enjoy learning React!'
};
}
如果您希望
备忘录
可以在其他方法中访问,您可能应该将let memos=…
替换为this.memos=…
。您不能在类
中使用let
、const
或var
。在ES2015中,您可以使用静态
方法或属性或方法,这些方法将转到原型
。
因此,要初始化类的属性,应该将其添加到构造函数中
class Person {
constructor(name){
this.name = name;
this.createdAd = new Date();
}
}
let bob = new Person('Bob');
bob.name // 'Bob'
你应该从学习ES6开始
class Person {
constructor(name){
this.name = name;
this.createdAd = new Date();
}
}
let bob = new Person('Bob');
bob.name // 'Bob'