为什么我在识别javascript上的类时遇到问题

问题描述 投票:-1回答:1

我如下创建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的错误

javascript class eslint jslint
1个回答
0
投票

您的代码存在一些问题:

  1. 类名应以印刷体字母开头,即使这可能不是导致出现错误消息的原因。
  2. constructor拼写错误
  3. 类方法不需要function关键字。

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

© www.soinside.com 2019 - 2024. All rights reserved.