Hi all,
I have taken tutorial from net. but i didnt understand one thing correctly
what does this

done:function(f){
            postaction=f || postaction //remember user defined callback functions to be called when images load
        }

and what does || sign mean there.
Thanks for attention!
here is

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
        }
    }
}
 
preloadimages(['1.gif', '2.gif', '3.gif']).done(function(images){
 //call back codes, for example:
 alert(images.length) //alerts 3
 alert(images[0].src+" "+images[0].width) //alerts '1.gif 220'
})

Hi Aze,

done:function(f){
  postaction = f || postaction //remember user defined callback functions to be called when images load
        }

This is a very convenient javascript shorthand but use it with caution.

In full (pseudocode)

  • if(f is not falsy), then postaction = f;
  • if(f is falsy), then leave postaction unaltered.

It is a way to provide the function with a default behaviour when called without an argument, in which case its formal variable f is would be undefined, and an error would occur further down in the function's code.

Because null , NaN , empty string "" and zero 0 are also falsy, passing anything that equates to any of these values will also give the default behaviour. This is a potential pitfall with the shorthand notation. Say zero (0) was a valid argument, then postaction = f || postaction; would behave incorrectly by leaving postaction unaltered instead of setting it to zero.

In cases where a falsy value is valid, then use the expression:

postaction = (typeof f !== 'undefined') ? f : postaction;

Airshow

Hi all,
I have taken tutorial from net. but i didnt understand one thing correctly
what does this

done:function(f){
            postaction=f || postaction //remember user defined callback functions to be called when images load
        }

and what does || sign mean there.
Thanks for attention!
here is

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
        }
    }
}
 
preloadimages(['1.gif', '2.gif', '3.gif']).done(function(images){
 //call back codes, for example:
 alert(images.length) //alerts 3
 alert(images[0].src+" "+images[0].width) //alerts '1.gif 220'
})

some piece of junk you've picked there
yet, this code is using some perverted version of my original "Freeze" variable method. But this is doing something else. (where did you get it from?!)

Its job is to make sure the argument values passed to the function are preserved one way or the other, although in this version they can be altered during recursion steps.

The "||" means (is a js logical) OR

And postaction = f || postaction means: "postaction" to be equal to "f" arg value OR to "postaction" (itself).

Thanks all for attention !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.