簡體   English   中英

乳清我在 javascript 上識別類時遇到問題

[英]Whey I have a problem to identify a class on javascript

我創建了一個如下所示的 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的錯誤the keyword 'Class' is reserved

您的代碼存在一些問題:

  1. 正如ema 所說,您需要將 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;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM