Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 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 Reply

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...