gpt4 book ai didi

javascript - 未捕获的类型错误 : object is not a function on object creation

转载 作者:行者123 更新时间:2023-12-03 09:57:00 25 4
gpt4 key购买 nike

function coordinate(x, y) {
this.x = x,
this.y = y
}

function generateBaracade(numBlocks) {
var coordinate = getRandomBaracadeCoordinate();
var xStyle = expandDirection.down;
var yStyle = expandDirection.right;
var xNumBlocks = findRandomFactor(numBlocks);
var yNumBlocks = numBlocks / xNumBlocks;

//figure out which way to expand the blocks
if (coordinate.x + xNumBlocks > xBlocks) {
xStyle = expandDirection.left;
} else {
xStyle = expandDirection.right;
}

if (coordinate.y + yNumBlocks > yBlocks) {
yStyle = expandDirection.down;
} else {
yStyle = expandDirection.up;
}

for (var i = 0; i <= xNumBlocks - 1; i++) {
for (var j = 0; j <= yNumBlocks - 1; j++) {
var tempBlock = Object.create(block);
tempBlock.type = "obstruction";
tempBlock.color = "grey";
tempBlock.illegalTerrain = true;
tempBlock.coordinate = new coordinate(coordinate.x + (i * xStyle), coordinate.y + (j * yStyle));
blockContainer[coordinate.x + (i * xStyle)][coordinate.y + (j * yStyle)] = tempBlock;
};
};
}

我收到“未捕获的类型错误:对象不是函数”:

tempBlock.coordinate = new coordinate(coordinate.x + (i * xStyle), coordinate.y + (j * yStyle));

这很奇怪,因为我遵循 Mozilla 指南来执行此操作:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

编辑:getRandomBaracadeCo整理的来源。 return 语句正是我想要做的,并且执行时没有错误。

function getRandomBaracadeCoordinate() {
var x = Math.floor((Math.random() * (xBlocks)));
var y = Math.floor((Math.random() * (yBlocks)));
return new coordinate(x, y);
}

最佳答案

您通过在 getBaracade 的第一行指定其他相同名称的内容来隐藏 coordinate 函数:

var coordinate = getRandomBaracadeCoordinate();

无论 getRandomBaracadeCooperative() 返回的都不是函数,因此 new坐标 会引发错误。

关于javascript - 未捕获的类型错误 : object is not a function on object creation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com