我的JavaScript文件可以';无法从另一个JavaScript文件中找到类
我对JavaScript还是有点陌生,我的问题是我在文件1中有一个类,我想在文件2中使用它,但当我尝试引用它“let something=new something();”时,我得到了一个错误,即没有定义某个类。我在HTML文件中用script标记引用了这两个脚本。我也在使用ES6和p5.js ` 这是HTML代码我的JavaScript文件可以';无法从另一个JavaScript文件中找到类,javascript,class,Javascript,Class,我对JavaScript还是有点陌生,我的问题是我在文件1中有一个类,我想在文件2中使用它,但当我尝试引用它“let something=new something();”时,我得到了一个错误,即没有定义某个类。我在HTML文件中用script标记引用了这两个脚本。我也在使用ES6和p5.js ` 这是HTML代码 <!DOCTYPE html> <html> <head> <script src="https://cdnjs.clo
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="matrixFunctions.js"></script>
<script src="sketch.js"></script>
</head>
</html>
如果你打算这样做,你需要确保
某些东西是全局公开的。不将其包装到函数中,或者从函数内部将其设置在窗口
对象上。或者由于某种原因(路径错误或其他原因)没有加载第二个文件
否则,共享JS文件和HTML的内容将更有利于解决问题
您的矩阵
类有语法错误
不能在类方法之前添加函数
关键字
class Matrix {
// makes and returns an empty matrix
constructor (rows, colums) {
if (typeof rows == "number" && typeof colums == "number") {
this.matrix = [];
for (var i = 0; i < colums; i++) {
this.matrix[i] = new Array(colums);
for (var j = 0; j < rows; j++) {
this.matrix[i][j] = 0;
}
}
return this.matrix
}
else {
let rowtype = typeof rows;
let columtype = typeof colums;
console.log('ERROR 1: Matrix was expecting numbers as arguments not ' + rowtype + ' and ' + columtype);
}
}
// adds random ints to the matrix
Randomize () {
for (var i = 0; i < this.matrix.length; i++) {
for (var j = 0; j < this.matrix[i].length; j++) {
this.matrix[i][j] = random();
}
}
}
// adds 2 arrays together or adds rows together
MatrixAdd (single, matrix) {
if (typeof single == 'boolean') {
if (single == true) {
for (var i = 0; i < this.matrix.length; i++) {
for (var j = 0; j < this.matrix[i].length - 1; j++) {
this.matrix[i][0] = this.matrix[i][0] + this.matrix[i][j + 1];
}
this.matrix[i] = this.matrix[i][0];
}
}
else if (single == false) {
console.log('I am currently working on this, please wait');
}
}
else {
let singletype = typeof single;
console.log('ERROR 2: MatrixAdd was expecting a boolean as first argument not ' + singletype);
}
}
}
类矩阵{
//生成并返回一个空矩阵
构造函数(行、列){
if(行的类型==“编号”&&typeof列==“编号”){
这个矩阵=[];
对于(变量i=0;i
尝试切换HTML文件中声明这两个脚本标记的顺序
声明它们的顺序很重要,因为如果文件2依赖于文件1中的类,那么必须首先声明文件1。使用doxument ready如何。只需在文件2中调用函数
document.addEventListener("DOMContentLoaded", function(){ // Handler when the DOM is fully loaded });
注意到这一点。由于我使用手机,我还没有测试代码
但是我想你可以试试看给我们看看你的HTML。@SLaks你的语法是无效的。查看开发工具控制台中的语法错误。@在控制台中,它不会显示html中的任何错误,只显示JavaScript。如果你能看到我在哪里编写了无效语法,那么你能告诉我,这样我就可以更正它了吗?我试过了,但没有任何更改,所以我想可能是我的类没有及时提交我试过,但我失败了仍然没有定义相同的错误矩阵我已经更新了我的帖子来解决你的语法错误。您可能会遇到未定义的错误,因为当JavaScript遇到语法错误时,它会停止处理脚本的其余部分。这意味着没有创建您的类。
class Matrix {
// makes and returns an empty matrix
constructor (rows, colums) {
if (typeof rows == "number" && typeof colums == "number") {
this.matrix = [];
for (var i = 0; i < colums; i++) {
this.matrix[i] = new Array(colums);
for (var j = 0; j < rows; j++) {
this.matrix[i][j] = 0;
}
}
return this.matrix
}
else {
let rowtype = typeof rows;
let columtype = typeof colums;
console.log('ERROR 1: Matrix was expecting numbers as arguments not ' + rowtype + ' and ' + columtype);
}
}
// adds random ints to the matrix
Randomize () {
for (var i = 0; i < this.matrix.length; i++) {
for (var j = 0; j < this.matrix[i].length; j++) {
this.matrix[i][j] = random();
}
}
}
// adds 2 arrays together or adds rows together
MatrixAdd (single, matrix) {
if (typeof single == 'boolean') {
if (single == true) {
for (var i = 0; i < this.matrix.length; i++) {
for (var j = 0; j < this.matrix[i].length - 1; j++) {
this.matrix[i][0] = this.matrix[i][0] + this.matrix[i][j + 1];
}
this.matrix[i] = this.matrix[i][0];
}
}
else if (single == false) {
console.log('I am currently working on this, please wait');
}
}
else {
let singletype = typeof single;
console.log('ERROR 2: MatrixAdd was expecting a boolean as first argument not ' + singletype);
}
}
}
document.addEventListener("DOMContentLoaded", function(){ // Handler when the DOM is fully loaded });