简体   繁体   中英

ReferenceError: […] is not defined

I am trying to use a JS function which is in an another JS file and I have this error :

ReferenceError: Lanceur is not defined

Lanceur is my object which is defined in my second file. I have a constructor :

public class Lanceur { 
  constructor(angleAiguille) {
    this.angleAiguille = angleAiguille;
  } // And functions .....

I have this line in my first file : lanceur = new Lanceur(0);

And I call my files in a HTML files with <script src="js/canvas.js" type="text/javascript"></script> , for example.

You need to create the class before you can create an instance of it. You also don't need the keyword public as browsers don't support it currently (Unless you're compiling this through Babel or something similar, but that wasn't obvious from your post) .

In your first file include the code that makes up the class, I've added a method as an example.

class Lanceur { 
  constructor(angleAiguille) {
    this.angleAiguille = angleAiguille;
  }

  someMethod() { 
   console.log('Firing') 
  }
}

You can then create an instance of it like so in your second file and call its methods.

const instance = new Lanceur;


// Calling a method...
instance.someMethod();

You can learn more about JavaScript class constructors here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM