gpt4 book ai didi

javascript - 函数未作为函数传递

转载 作者:行者123 更新时间:2023-11-30 13:09:59 26 4
gpt4 key购买 nike

我正在开发一个简短的“引擎”类,以在一定程度上简化与 Three.js 的交互。我将示例的 Render() 方法重构为这个 Engine JS 类,如下所示:

var Engine = function () {
var pub = {},
scene,
camera;

// SNIP: Extra private and public variables and functions...

pub.Render = function (fixedUpdate) {
requestAnimationFrame(pub.Render);
fixedUpdate();
renderer.render(scene, camera);
};

return pub;
}();

我尝试通过将一些函数与我想要执行的操作传递给它来使用它。例如:

Engine.Render(function () {
cube.rotation.x += 0.01;
cube.rotation.y += 0.02;
});

但是,当我尝试在 Chrome 中运行它时,出现错误:Uncaught TypeError: number is not a function,并且在检查 Engine.Render 的 fixedUpdate 变量时,它通常是一些非常大的数字,并且显然没有注册为调用代码传入的匿名函数。

作为完整性测试,我尝试将句柄传递给非匿名函数,如下所示:

function animation() {
cube.rotation.x += 0.01;
cube.rotation.y += 0.02;
}

Engine.Render(animation);

...并得到完全相同的结果。我怀疑问题出在我调用参数的方式上。

最佳答案

试试这个:

  pub.Render = function (fixedUpdate) {
requestAnimationFrame(function () {pub.Render();});
fixedUpdate();
renderer.render(scene, camera);
};

关于javascript - 函数未作为函数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243963/

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