首页 > 解决方案 > 我的 JavaScript 文件无法从另一个 JavaScript 文件中找到类

问题描述

我对 JavaScript 还是有点陌生​​,我的问题是我在文件 1 中有一个类,我想在文件 2 中使用它,但是当我尝试引用它时“let something = new Something();” 我收到错误未定义的东西。我已经在我的 HTML 文件中使用 script 标签引用了这两个脚本。我也在使用 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>

标签: javascriptclass

解决方案


如果你打算这样做,你需要确保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);
        }
    }
}

推荐阅读