简体   繁体   English

javascript和空返回对象中的匿名函数

[英]Anonymous functions in javascript and empty return objects

Below is the source code for a function that preloads images onto a page, the author has added in some comments to explain how the code works, but I still do not fully understand all of it. 下面是将图像预加载到页面上的函数的源代码,作者在一些注释中添加了解释代码如何工作的内容,但我仍然没有完全理解它的全部内容。 to be specific, He claims that the return value of this function is an empty object with a "done()" method that calls the predefined anonymous function, "postaction()". 具体而言,他声称此函数的返回值是一个空对象,其中“done()”方法调用预定义的匿名函数“postaction()”。 Is the user of this code supposed to enter his/her own code into the empty postaction function on line 2? 该代码的用户是否应该将他/她自己的代码输入到第2行的空事后处理函数中? If that's how it works, then what does "postaction=f || postaction" in the return object do? 如果它是如何工作的,那么返回对象中的“postaction = f || postaction”是做什么的呢?

Source code: 源代码:

function preloadimages(arr){
  var newimages=[], loadedimages=0
  var postaction=function(){}
  var arr=(typeof arr!="object")? [arr] : arr
  function imageloadpost(){
      loadedimages++
      if (loadedimages==arr.length){
          postaction(newimages) //call postaction and pass in newimages array as parameter
      }
  }
  for (var i=0; i<arr.length; i++){
      newimages[i]=new Image()
      newimages[i].src=arr[i]
      newimages[i].onload=function(){
          imageloadpost()
      }
      newimages[i].onerror=function(){
        imageloadpost()
      }
  }
  return { //return blank object with done() method
      done:function(f){
          postaction=f || postaction 
          //remember user defined callback functions to be  called when images load
    }
  }
}

link to author's page:http://www.javascriptkit.com/javatutors/preloadimagesplus.shtml 链接到作者的页面:http://www.javascriptkit.com/javatutors/preloadimagesplus.shtml

You can enter your own function for postaction, although it's not a callback function like you might expect. 你可以为postaction输入你自己的函数,虽然它不是你想象的回调函数。

However, it returns an object with a done function in it. 但是,它返回一个带有done函数的对象。

If you do soth. 如果你做的话。 like 喜欢

 preloadimages().done(function () {
     console.log('done')
 });

your function will be executed. 你的功能将被执行。 If you don't provide the fnction as a parameter of done, the default postaction will be called and do nothing, since it is an empty function 如果你没有提供fnction作为done的参数,那么默认的postaction将被调用并且什么都不做,因为它是一个空函数

From reading that code, it looks like you're not meant to redefine postaction() - in fact, you shouldn't have to modify any of this code at all. 从阅读该代码看起来,您似乎并不打算重新定义postaction() - 事实上,您根本不需要修改任何代码。 You are actually expected to pass a function as an argument to the done() function, which will be called later on successful image load (a 'callback' function). 实际上,您希望将函数作为参数传递给done()函数,稍后将在成功的图像加载(“回调”函数)上调用该函数。

This line: 这一行:

postaction=f || postaction

means "assign the value of f to postaction if f has a value, otherwise assign postaction to itself" (ie. don't change it). 意思是“指定f的值postaction如果f有一个值,以其他方式转让postaction本身”(即不改变的话)。

I don't know what exactly this code is used for, but the returned object has a function done which can be used with or without function parameter. 我不知道这个代码究竟用于什么,但返回的对象有一个功能完成,可以使用或不使用函数参数。

Within done, this statement 完成后,这个声明

f || postaction

Meaning f OR postaction returns f if f is not null/undefined or postaction else, so if you call 含义f OR postaction返回f如果f不是null / undefined或postaction,那么如果你调用

myreturnObject.done();

this evaluates to 这评估为

postaction = postaction

because f is not define. 因为f没有定义。 If you call 如果你打电话

myreturnObject.done(function(newImages) { ... });

this evaluates to 这评估为

postaction = f.

Postaction is then used within the for loop in the imageLoadPost function. 然后在imageLoadPost函数的for循环中使用Postaction。 If you want some own coding there, you can pass it to the done method as parameter as shown. 如果你想在那里有一些自己的编码,你可以将它作为参数传递给done方法,如图所示。 If you do not need any additional coding there, do not pass an function to postaction. 如果您不需要任何其他编码,请不要将函数传递给postaction。 The empty postaction function defined as fallback will be called then. 然后将调用定义为回退的空事后处理函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM