我的 JavaScript 文件找不到另一个 JavaScript 文件中的类



我对 JavaScript 仍然有点陌生,我的问题是我在文件 1 中有一个类,我想在文件 2 中使用它,但是当我尝试引用它时"让某物 = 新的东西((;"我收到错误未定义某些内容。我已经在我的 HTML 文件中引用了带有脚本标签的两个脚本。我也在使用 ES6 和 p5.js `

"use strict";
class Matrix
{
// makes and returns an empty matrix
function 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
function 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
function 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);
}
}
}

'

那是我想要使用的类的文件 1 这是文件 2

"use strict";
let matrix;
function setup()
{
let matrix = new Matrix(4, 5);
matrix.Randomize();
matrix.MatrixAdd(true);
console.log(matrix);
}

这是 HTML 代码

<!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>

尝试切换在 HTML 文件中声明这两个脚本标记的顺序。

声明它们的顺序很重要,因为如果文件 2 依赖于文件 1 中的类,则必须首先声明文件 1。

如果你要这样做,你需要确保Something是全球公开的。要么不将其包装在函数中,要么从函数内部将其设置在window对象上。或者您的第二个文件由于某种原因未加载(路径错误或其他原因(

否则,共享JS文件和HTML的内容将更有利于解决问题。

Matrix类存在语法错误。

不要在类方法之前添加function关键字。

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);
}
}
}

使用Doxument ready怎么样。 只需在 file2 中调用函数

document.addEventListener("DOMContentLoaded", function(){ // Handler when the DOM is fully loaded });

由于我使用移动设备,我还没有测试代码。

但我认为你可以尝试一下

最新更新