我对JavaScript还是有点陌生,我的问题是我在文件1中有一个类,并且想在文件2中使用它,但是当我尝试引用它时,“让某些东西= 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>
答案 0 :(得分:0)
如果要这样做,则需要确保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);
}
}
}
答案 1 :(得分:0)
尝试并切换在HTML文件中声明这两个脚本标签的顺序。
声明它们的顺序很重要,因为如果文件2取决于文件1中的类,那么必须首先声明文件1。
答案 2 :(得分:0)
如何使用准备好的剂量。只需在file2内部调用函数
document.addEventListener("DOMContentLoaded", function(){ // Handler when the DOM is fully loaded });
注意。由于我正在使用移动设备,因此我尚未测试代码。
但我认为您可以尝试