简体   繁体   English

ReferenceError:[…]未定义

[英]ReferenceError: […] is not defined

I am trying to use a JS function which is in an another JS file and I have this error : 我正在尝试使用另一个JS文件中的JS函数,但出现此错误:

ReferenceError: Lanceur is not defined ReferenceError:未定义Lanceur

Lanceur is my object which is defined in my second file. Lanceur是我的第二个文件中定义的对象。 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); 我的第一个文件中有这一行: 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. 例如,我将我的文件称为HTML文件,其<script src="js/canvas.js" type="text/javascript"></script>

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) . 您也不需要public关键字,因为浏览器当前不支持该关键字(除非您通过Babel或类似方法进行编译,但这在您的帖子中并不明显)

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 . 您可以在此处了解有关JavaScript类构造函数的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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