Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

syntax - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...