Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Syntax with [...] in front of a function call / constructor call

Tags:

javascript

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

like image 862
user3457016 Avatar asked May 26 '15 07:05

user3457016


People also ask

How do you call a constructor from a function?

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.

How do you create function using function constructor in JavaScript?

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.

What is constructor () in JS?

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.

Can we call a function inside a constructor?

You can call a virtual function in a constructor. The Objects are constructed from the base up, “base before derived”.


1 Answers

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.

like image 127
doldt Avatar answered Nov 14 '22 21:11

doldt