javascript - 为什么要传入一个函数?

标签 javascript

loader.load('scene.gltf', function(gltf){
  car = gltf.scene.children[0];
  car.scale.set(0.5,0.5,0.5);
  scene.add(gltf.scene);
  animate();
});

我在浏览文档时发现了这段代码。我们为什么以及如何传递一个完整的函数作为参数。我是 JS 新手,所以请指导我正确的方法。 :)

最佳答案

根据三个.js documentation :

.load ( url : String, onLoad : Function, onProgress : Function, onError : Function ) : null

url — the path or URL to the file. This can also be a Data URI.
onLoad — Will be called when load completes. The argument will be the loaded object.
onProgress — Will be called while load progresses. The argument will be the XMLHttpRequest instance, which contains .total and .loaded bytes.
onError — Will be called when load errors.  

Begin loading from url and call onLoad with the parsed response content. 

因此第二个参数必须是 Function 类型,通常称为“回调”,在这种特殊情况下,它将在资源加载完成时执行。

回调函数是许多编程语言(包括 JavaScript)中的常见模式。它们充当在给定时间/当某件事正在发生/已经发生时执行代码的一种方式。

关于javascript - 为什么要传入一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65613374/

相关文章:

javascript - 有太多的同时 AJAX 连接不好吗?

javascript - 使用 2 个条件对 JS 对象数组进行排序

javascript - 单页结账未继续下一步

javascript - 如何访问json数据属性

javascript - ColdFusion float 问题

javascript - 如何在不从 firebase 重新加载页面的情况下更新数据

javascript - 当遇到返回之前执行到达 block 末尾时,在 Parse Promise then block 中会发生什么?

javascript - 在 Vue 中编译内联文本字符串

javascript - 尝试使用 slider 更改 div 背景颜色

javascript - 如何通过数组无限循环切换类列表?