Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么要传入函数?_Javascript - Fatal编程技术网

Javascript 为什么要传入函数?

Javascript 为什么要传入函数?,javascript,Javascript,我在浏览文档时发现了这段代码。为什么以及如何将完整函数作为参数传递。我是JS新手,所以请引导我走正确的路。:) 根据三个.js: 因此,第二个参数必须是Function类型,通常称为“callback”,在这种特殊情况下,将在资源加载完成时执行 回调函数是许多编程语言(包括JavaScript)中的常见模式。它们是在给定的时间/事情发生/已经发生时执行代码的一种方式。函数与任何其他JavaScript一样是一个值。也许将函数看作可调用对象会有所帮助。要理解为什么loader.load接受一个函数

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

根据三个.js:

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


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

函数与任何其他JavaScript一样是一个值。也许将函数看作可调用对象会有所帮助。要理解为什么
loader.load
接受一个函数,您必须查看它的实现。它可能会初始化一些东西,函数允许调用方告诉加载程序,一旦初始化完成,就应该执行这些代码。您可能希望从更多的入门教程和练习开始。如果有人能提供一些关于将函数作为参数传递的文档,这将非常有用subjec@bruh_moment不完全是文件。我的猜测是,它解析文件并将其内容的一些表示形式传递给回调函数,
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();
});
.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.