What is the double-dot operator (..) in Javascript

2019-01-23 05:15发布

问题:

I'm doing some work with the Parser API in Spidermonkey. The docs show that there's a binary operator ... Anybody have any idea what this is/does? I'd love to know. I've never seen it before. If I were forced to guess, I'd have to say it's something with E4X, but that's only because I know nothing about E4X.

回答1:

It is an E4X operator.

From https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide:Processing_XML_with_E4X:

While the . operator accesses direct children of the given node, the .. operator accesses all children no matter how deeply nested:



回答2:

Not to be confused with the decimal point and dot:

var val= 1000..toExponential()


回答3:

Something like:

255..toString(16);

First dot is actually a decimal point, just let JavaScript Compiler know the second dot wants to invoke property or method. And 255.toString(16) makes JavaScript Compiler confused(identifier starts immediately after decimal numeric literal).



回答4:

It is indeed E4X. It does the same thing as the single dot operator, which selects children, but it selects all descendants. (It's by analogy with XPath's / operator selecting children of an element that match the selector and // selecting all descendants that match the selector.)