Short circuit

Yesterday I remembered the following code:

navigator.getUserMedia = navigator.getUserMedia
|| navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia
|| navigator.msGetUserMedia;

I couldn’t understand how it works, I mean shouldn’t it be something like this:

if (!navigator.getUserMedia)
{
if (navigator.webkitGetUserMedia){
navigator.getUserMedia = navigator.webkitGetUserMedia;}
else{
if (navigator.mozGetUserMedia){
navigator.getUserMedia = navigator.mozGetUserMedia;}
else
{if (navigator.msGetUserMedia)
navigator.getUserMedia = navigator.msGetUserMedia;}
}
}

After some research Short circuit evaluation came up. So this code actually performs the task in a more elegant way. Now I have more reading to do because Python has short circuit evaluation also.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s