我一直在尝试使用RequireJS来尝试帮助整理我的代码。我只是有一个关于如何从另一个脚本文件中调用某些变量的快速问题。
我的主要脚本结构如下:
require([
'game',
'piece',
], function(
Game,
Piece,
) {
var game = new Game();
var piece = new Piece();
game.start();
piece.start();
});
我想从myice.js脚本中的game.js脚本中调用gridArray变量。
我确实尝试过这样做,但它在控制台内部返回未定义
define([
'game'
], function(
Game
) {
function Piece(piece_type, current_row, current_col, current_status)
{
this.piece_type = piece_type;
this.current_row = current_row;
this.current_col = current_col;
this.current_status = current_status;
}
Piece.prototype.start = function()
{
this.king();
console.log(Game.gridArray)
}
Piece.prototype.king = function()
{
var canvas = document.getElementById("chessBoard")
var ctx = canvas.getContext("2d")
ctx.beginPath();
ctx.fillStyle = "black"
ctx.fillRect(12.5, 12.5, 50, 50)
}
return Piece;
});
在此函数的game.js中调用gridArray
function Game()
{
this.canvas;
this.ctx;
this.row = ["a", "b", "c", "d", "e", "f", "g", "h"];
this.gridArray = [];
}