I have found a syntax in javascript I have never seen before and I was unable to find the right documentation.
It comes from a tutorial:
var connection = new [webkit|moz]RTCPeerConnection(...)
I can find by myself what webkit means and what moz means, presumably these are two defined constants or enums.
but my question is:
What does the syntax [webkit|moz]
mean in those squared brackets?
Is is some kind of typecasting the result of the function result?
And what does the |
character mean in [webkit|moz]
- is this the OR operator?
thx
Invoking a constructor from a method No, you cannot call a constructor from a method. The only place from which you can invoke constructors using “this()” or, “super()” is the first line of another constructor. If you try to invoke constructors explicitly elsewhere, a compile time error will be generated.
In the above example, function Person() is an object constructor function. To create an object from a constructor function, we use the new keyword. Note: It is considered a good practice to capitalize the first letter of your constructor function.
A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.
You can call a virtual function in a constructor. The Objects are constructed from the base up, “base before derived”.
That's not proper javascript syntax (if you try running it, you'll get an unexpected token error at the first [
), It just means you have to use either in your code, ie.:
new mozRTCPeerConnection()
for Firefox, and
new webkitRTCPeerConnection()
for webkit based browsers.
See the MDN docs on RTCPeerConnection:
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers.
and
Warning: RTCPeerConnection and RTCSessionDescription are currently prefixed in most browsers. You should include a polyfill if you're using it in any work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With