乳清我在识别 javascript 上的类时遇到问题

Posted

技术标签:

【中文标题】乳清我在识别 javascript 上的类时遇到问题【英文标题】:Whey I have a problem to identify a class on 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 是的 【参考方案1】:

您的代码存在一些问题:

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

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

【讨论】:

以上是关于乳清我在识别 javascript 上的类时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章