我想在我指定的位置调用 ctx.fillRect(10, 10, 15, 5)
(Array[0]
)我 console.log(Array[0])
它显示数组内部的函数,但当我指定数组的索引时它不会调用该函数。
function translate1() {
var ctx = canvas.getContext("2d");
var Array = [
function() {ctx.fillRect(10, 10, 15, 5)}
];
console.log(Array[0]); // displays as expected here
Array[0]; // I want the function to be called here
ctx.transform(1, 0, 0, 1, 0, 20);
Array[0]; // and again here
ctx.transform(1, 0, 0, 1, 0, 20);
ctx.fillRect(10, 10, 15, 5);
ctx.transform(1, 0, 0, 1, 0, 20);
ctx.fillRect(10, 10, 15, 5);
ctx.transform(1, 0, 0, 1, 0, 20);
ctx.fillRect(10, 10, 15, 5);
ctx.transform(1, 0, 0, 1, 0, 20);
}
最佳答案
您需要使用 ()
调用该函数
Array[0]()
由于该函数没有 return
这样做并没有多大好处console.log(Array[0]())
它将显示 undefined
关于javascript - 如何显示(或调用)在 javascript 数组内部定义的匿名函数的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50081626/