Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ‘::’ (double colon) do in JavaScript?

The documentation of some JavaScript APIs shows the following snippets as an example of how to invoke some function:

<button type="button" onClick="foo.DoIt(72930)">Click</button>

<button type="button" onClick="foo.DoIt(42342::37438)">Click</button>

:: is obviously used here to allow either one or two arguments to be passed to the function.

What does :: do in JavaScript?

And how does the function know if one or two values were passed? How does it read them?


On closer look, the examples show other weird stuff like

<button type="button" onClick="foo.Bar(72//893)">Click</button>

<button type="button" onClick="foo.Qux(425;1,34::)">Click</button>

At least the // looks just wrong.

So I guess it's not some fancy new syntax that I'm not aware of, but maybe the examples are just missing quotes around a single string argument.

like image 442
dtb Avatar asked Oct 05 '09 14:10

dtb


People also ask

What does :: do in JavaScript?

The colon symbol ( : ) is generally used by JavaScript as a delimiter between key/value pair in an object data type. For example, you may initialize an object named car with key values like brand and color as follows: let car = { brand: "Toyota", color: "red", };

What does a double colon mean in code?

The double colon ( :: ) may refer to: an analogy symbolism operator, in logic and mathematics. a notation for equality of ratios. a scope resolution operator, in computer programming languages.

What is the use of double colon in C?

The Scope Resolution Operator in C++ Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.


4 Answers

It was certainly not the case at the time of your question, but right now :: is a valid ES7 operator. It's actually a shortcut for bind.

::foo.bar

is equivalent to

foo.bar.bind(foo)

See an explanation here for examples:

like image 61
Brann Avatar answered Oct 07 '22 05:10

Brann


Nothing. It is a syntax error.

>>> alert(42342::37438)
SyntaxError: missing ) after argument list
like image 41
Quentin Avatar answered Oct 07 '22 03:10

Quentin


:: has nothing to do with the number of parameters. You can do that already in JavaScript with a normal comma:

function SomeFunction(param1, param2) {
   //...
}

SomeFunction('oneParam'); // Perfectly legal

Also, based on Tzury Bar Yochay's answer, are you sure you're not looking at something like the following?

$('this::is all one::parameter'); // jQuery selector
like image 7
Joel Coehoorn Avatar answered Oct 07 '22 04:10

Joel Coehoorn


In which example did you see that? So far, JavaScript does not have a double colon operator!

The double colon replaced the single-colon selectors for pseudo-elements in CSS3 to make an explicit distinction between pseudo-classes and pseudo-elements. But that is CSS3, not JavaScript! Not At ALL!

like image 2
Tzury Bar Yochay Avatar answered Oct 07 '22 03:10

Tzury Bar Yochay