954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with || sign

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'
})
azegurb
Posting Whiz in Training
244 posts since Sep 2009
Reputation Points: 11
Solved Threads: 2
 

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

Airshow
WiFi Lounge Lizard
Moderator
2,682 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

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).

Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
 

Thanks all for attention !

azegurb
Posting Whiz in Training
244 posts since Sep 2009
Reputation Points: 11
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You