【问题标题】:Whey I have a problem to identify a class on javascript乳清我在识别 javascript 上的类时遇到问题
【发布时间】:2020-04-27 12:06:10
【问题描述】:

我创建了一个 JavaScript class,如下所示:

class contact{
   constructeur(nom,prenom){
     this.nom = nom;
     this.prenom = prenom;
   }     

   function afficher(){
     return "Nom : " + this.nom + "Prenom : " + this.prenom;
   }

...

但我在 jslint Excepted an identifier saw 'class' 中有错误

而在 eslint 中,the keyword 'Class' is reserved 出现错误

【问题讨论】:

  • 函数应该在类中吗?
  • 你是否在 ESLint 中启用了 ES6 语法?应该有一个选项ecmaVersion
  • 类的构造函数也应该用英文拼写,而不是外语。
  • 是的,我已经在工具栏中设置了 ecmaVersion 选项
  • @Addis 是的

标签: javascript class eslint jslint


【解决方案1】:

您的代码存在一些问题:

  1. 作为ema says,您需要将 esversion 设置为 6。
  2. 类名应该以大写字母开头,即使这可能不会导致错误消息。
  3. constructor 拼写错误
  4. 类方法不需要function 关键字。
/*jshint esversion:6 */

class Contact {
  constructor (nom, prenom) {
      this.nom = nom;
      this.prenom = prenom;
  }
  afficher () {
      return "Nom : " + this.nom + " Prenom : " + this.prenom;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多