1

I am reading a open source project and it has a js file like this

var pipe = function(source, listeners){
    source.onmessage = function onmessage(msg){
           //.. do something ..
    };

    return {
      //.. something ...
    };
  }(nativeWindow || appjs, {});

What does (nativeWindow || appjs ) means where they both are objects ?

Does it mean that this function is called for both of them ?

Or it is merging both of these objects ?

And is this .onmessage is something standard thing like alert() or window ? What i mean by standard is that is there something happening behind the scenes in this .onmessage function or is it just like simple function ?

Thanks

Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • 2
    The answer : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators – Denys Séguret Sep 10 '13 at 10:59
  • This might be easy for others but for somebody with c++ background who is trying to understand a javascript file, it seems confusing. – Ashish Negi Sep 10 '13 at 11:04
  • Thanks you all. can you have a look at http://help.dottoro.com/ljjqtjsj.php and tell your final comments on this question. – Ashish Negi Sep 10 '13 at 11:10
  • But you're asking about 4 different questions here. Your question seems to be 'talk me through this JavaScript' – UpTheCreek Sep 10 '13 at 11:10
  • @JonathanLonowski Thanks but i wanted to know that where i should search about this onmessage event firing in the framwork if its their thing or if its something like addEventListener like standard thing then i would not be able to view whats happening in their code ? – Ashish Negi Sep 10 '13 at 11:13

5 Answers5

5

if nativeWindow evaluates to false (e.g. if it's undefined) then appjs is passed in as source; otherwise nativeWindow is passed in as source. The property onmessage of whichever object ispassed in as source is then set to the onmessage function

Matthew Mcveigh
  • 5,695
  • 22
  • 22
  • so onmessage is simple function, nothing like some standard function that would be invoked if something happens on dom ? – Ashish Negi Sep 10 '13 at 11:06
  • No, it's not a simple function, please read about [Post Messages](https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage) – Teemu Sep 10 '13 at 11:08
  • so that must be framework thing not of any standard thing of browsers. can you have a look at http://help.dottoro.com/ljjqtjsj.php and tell your final comments on this question. – Ashish Negi Sep 10 '13 at 11:09
  • 2
    @ASHISHNEGI `onmessage` is very standard property, no frameworks needed to use it. – Teemu Sep 10 '13 at 11:11
2

From the MDN :

expr1 || expr2

Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

In this specific case, the first value whose value is defined is passed as argument.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

.onmessageis whatever it is assigned in the pipe function. It is just a normal property on the source object.

The || and && in javascript returns a value. This means; nativeWindow OR appjs gets passed in, as the returned value from the expression; the last evaluated expression gets returned. If nativeWindowis a falsy (null, undefined etc) value, appjs gets passed in. If nativeWindow is thruthy (e.g. an object) it gets passed in. Just think this OR that. If both values are falsy, the one on the right hand side will still be passed in. Read more on || and && here: http://www.grauw.nl/blog/entry/510

ptf
  • 3,210
  • 8
  • 34
  • 67
1

|| is the OR operator. It also short-circuits if the left-hand evaluates is true-ish. So what it does is provide the function with either the value of nativeWindow OR appjs if nativeWindow is false-ish.

A more verbose of the same would be:

var pipe = function()...;
if(nativeWindow) {
    pipe = pipe(nativeWindow);
} else {
    pip = pipe(appjs);
}
Bouke
  • 11,768
  • 7
  • 68
  • 102
1
(nativeWindow || appjs, {}) = (source, listeners) 

They are the parameters passed

nativeWindow  || appjs 

means if nativeWindow is undefined or falsy take the value of appjs

source.onmessage  

onmessage is the property of the object source (passed as nativeWindow || appjs) which holds a function in your case