javascript - 函数未作为函数传递

标签 javascript three.js anonymous-function

我正在开发一个简短的“引擎”类,以在一定程度上简化与 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/

相关文章:

javascript - 在promise内部出错后继续子循环-try/catch

javascript - 使用 OO Javascript、ThreeJS 和 ScrollMagic 的网络 worker

three.js - 使用 meshStandardMaterial Three.js 无法正确渲染纹理

PHP 闭包给出了奇怪的性能行为

javascript - 自动执行匿名 JavaScript 函数的括号位置?

javascript - 将鼠标悬停在所有列表项上,指定项除外

javascript - Angular 2 - 找不到模块 ' ng-factory'

javascript - 默认情况下,BabelJS 不会转译 ES6 语法的匿名函数?

javascript - 有什么方法可以使用 jQuery 从选择的项目中获取标签吗?

javascript - 无法让 Three.js TrackballControl 缩放工作