Javascript syntax with [...] before function call / constructor call

I found syntax in javascript that I had never seen before, and I could not find the correct documentation.

This is from the tutorial:

var connection = new [webkit|moz]RTCPeerConnection(...) 

I can find for myself what webkit means and what moz means, presumably these are two specific constants or enumerations.

but my question is:

What does the [webkit|moz] syntax in square brackets mean?

Is there any type of casting result to the result of a function?

And what does the symbol | in [webkit|moz] is the OR operator?

THX

+5
source share
3 answers

This is not the correct javascript syntax (if you try to run it, you will get an unexpected token error in the first [ ). It just means that you should use either in your code, i.e. .:

 new mozRTCPeerConnection() 

for firefox and

 new webkitRTCPeerConnection() 

for webkit-based browsers.

See the MDN RTCPeerConnection docs :

Since this technology specification has not stabilized, check the compatibility table for the correct prefixes for use in different browsers.

and

Warning: RTCPeerConnection and RTCSessionDescription are currently prefixed in most browsers. You must enable polyfill if you use it in any job.

+4
source

Like @ doldt said this is not js syntax, but just a pseudo-code pointer. You can use a kind of polyfill to create the correct version:

 var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; 

A source

+2
source

This was taken from the Mozilla dev network:

Warning: RTCPeerConnection and RTCSessionDescription are currently a prefix in most browsers. You must enable polyfill if you are using it in any job. For instance:

 var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; 

Take a look at the documentation at: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection

+2
source

All Articles