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/