3

I'm currently maintainging some DS/JS code in Demandware and I just found that :

var XmlReponse : XML = new XML(result.object.text);
status = XmlReponse.@["STATUS"];

What does this unusual "@[...]" syntax do ?

It could have something to do with ECMA-357 specification (aka "E4X"), but I'm not sure : in the DW docs (needs credentials), it is said :

"You can read values from an XML object the same way you would from standard ECMAScript objects"

var quantity = order.item(0).@quantity;
var singleItem = order.item.(@quantity == 1);

That seems to explain the @ but let me skeptical about the following square brackets, maybe some kind of dynamic property ?

Thanks :)

Benj
  • 1,184
  • 7
  • 26
  • 57
  • 1
    Is that really plain JavaScript? What is the name of the file you found that in? More specifically what is its suffix/extension? Is it `.js`? I wonder because JavaScript doesn't have type-annotations either, which is in the line before the one you wonder about. – Some programmer dude Aug 11 '17 at 08:04
  • Not really pure JS, it is Demandware script, but it is meant to be ES5. I'll update. – Benj Aug 11 '17 at 08:06
  • 1
    @Benj it's not valid ES5 for sure. – Bergi Aug 11 '17 at 08:07
  • 1
    And you're not using type-checkers like [Flow](https://flow.org/) (which can add additional syntax)? – Some programmer dude Aug 11 '17 at 08:07
  • In [the Demandware tag wiki](https://stackoverflow.com/tags/demandware/info) it says that Demandware "... utilizes a JavaScript-***style*** scripting language..." (emphasis mine). Maybe it's not *real* JavaScript but its own dialect that adds additional syntax? – Some programmer dude Aug 11 '17 at 08:10
  • I don't know if we are using Flow or something else, with Demandware "the truth is out of there" – Benj Aug 11 '17 at 08:10
  • @Someprogrammerdude Yes, it seems to be some custom stuff, but I can't help, to find it in their docs... Whatever, thanks for the advice, i now know that it is not JS and could be processed by some external tool/lib. – Benj Aug 11 '17 at 08:12

1 Answers1

2

From reading the manual it seems to read a property of an xml element. I don't see that reading the indexer would be any different

Your mentioned code would then read the attribute STATUS on the xmlresponse

The code on the linked ex4 manual status:

E4X allows you to access the attributes of a particular element with the .@ operator. The most basic case would look something like.

var element = <foo bar="1"/>
element.@bar = 2;
Icepickle
  • 12,689
  • 3
  • 34
  • 48
  • And I suppose the `[...]` is for accessing only an attribute ? Would be `` ? Why these square brackets ? – Benj Aug 11 '17 at 08:53
  • @Benj I think it wouldn't really matter, the @ sign already points to an attribute, I guess you could just write `@status` as well, didn't find anything in the docs. Interestingly it is written with only uppercases, though xml spec normally says that all attributes should be lowercased – Icepickle Aug 11 '17 at 09:00
  • Yes, it is very strange. Thanks for your answer & Have a nice day :) – Benj Aug 11 '17 at 09:20