31

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dtb
  • 213,145
  • 36
  • 401
  • 431

8 Answers8

282

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:

Zearin
  • 1,474
  • 2
  • 17
  • 36
Brann
  • 31,689
  • 32
  • 113
  • 162
  • 1
    If I'm reading that correctly, then it isn't valid in the context given in the question. The LHS and RHS of the `::` in the question are Numbers. – Quentin Jul 27 '15 at 07:49
  • 58
    This is the answer I was looking for when I Googled for what :: means though! Thanks – poshaughnessy Jul 30 '15 at 13:46
  • 2
    This answer has nothing to do with the question, because (1) ES7, which is published in June 2016, doesn't contain `::` operator; (2) `::` operator is only a stage 0 proposal currently, which is highly probable of being changed or removed; (3) In the context of the question, it is not a valid `::` operator syntax, and even the correct syntax will not work in any current major browsers. – rhgb Nov 14 '16 at 08:44
  • 6
    The question title doesn't match the question body, although, this answer suits the title question, in my case what I was looking for. – andrepaulo Jan 13 '17 at 13:56
15

Nothing. It is a syntax error.

>>> alert(42342::37438)
SyntaxError: missing ) after argument list
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 3
    Yep, unless for some ungodly reason said developer is actually parsing the onclick attribute value with `document.getElementsByTagName('button')[0].getAttribute('onclick')` and extracting that information, that is an invalid ECMAScript expression. – meder omuraliev Oct 05 '09 at 14:30
  • 1
    @Brann — It doesn't appear to be valid in the context given in the question. – Quentin Jul 03 '16 at 15:04
  • @Pawel — No it isn't. See my previous comment. See the context of the question. – Quentin Jul 14 '17 at 14:10
7

:: 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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I guess you're right and it's missing the quotes. I will submit a bug report to the author :) – dtb Oct 05 '09 at 14:26
2

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!

Tzury Bar Yochay
  • 8,798
  • 5
  • 49
  • 73
1

It could be using ECMAScript for XML (ECMA-357 standard) which would imply the double quotes are a XPath operator.

See ECMAScript for XML

Jeff Lewis
  • 1,081
  • 11
  • 10
1

It must be a typo for

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

<button type="button" onClick="foo.DoIt('42342::37438')">Click</button>
Xinus
  • 29,617
  • 32
  • 119
  • 165
0

Perhaps it's a typo, and the whole thing is expected to be in quotes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
0

I am guessing that the parameter list for foo.DoIt() is generated by code, and one the values was empty.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283