1

What does the two colons before the toString() method invocation mean?

For instance, I found the following code:

orderXml.ns::['status'].toString();

Is it the same as ['status'].toString().call(orderXml.ns);?

I am using the Demandware Script above.

Thank you.

4

1 回答 1

3

In Demandware Script you'll find that a long-deprecated concept called E4X which extends the JavaScript 1.6/ES3 syntax still exists. However, it's usage is now discouraged. You can find some documentation for this syntax here: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X/Processing_XML_with_E4X

In the script snippet you provided, the colons are used to identify the XML namespace of the following expression. Without that namespace, you may find that the incorrect object is referenced or you may get an undefined reference. See the specific area of the documentation archive linked above that pertains to Namespaces: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X/Processing_XML_with_E4X#Handling_namespaces

The code essentially looks for an object property with the name: status. This isn't actually Array notation as it appears upon cursory inspection. Specifically, it looks for a namespaced property. It would not be the same as calling:

['status'].toString().call(orderXml.ns);

The .toString() method is used to ensure we get the String representation of the property rather than a reference to an instance of that XML Node.

Please note that Demandware uses a modified version of the Mozilla Rhino 1.7R5 JavaScript implementation. See feature matrix here: https://mozilla.github.io/rhino/compat/engines.html

于 2019-04-10T17:26:01.543 回答