-1

I have a little experience programming in other languages but I am learning Javascript.

I am seeing this line

 var runningAmount  = 42;
 var subTotalDescr  = "Test Goodies";

 var paymentRequest = {
   currencyCode: 'USD',
   countryCode: 'US',
   requiredShippingContactFields: ['postalAddress','email', 'name', 'phone'],
   lineItems: [{label: subTotalDescr, amount: runningAmount }],
   total: {
      label: 'Shoes',
      amount: getTotal()
   },
   supportedNetworks: ['amex', 'masterCard', 'visa' ],
   merchantCapabilities: [ 'supports3DS', 'supportsEMV', 'supportsCredit', 'supportsDebit' ]
};

I see that elements as subTotalDescr and runningAmount are other variables and getTotal() is a function.

So, these elements will be read and compose paymentRequest var, but what exactly is paymentRequest? A JSON object, an associative array?

Excuse me if the question is stupid. thanks.

Gluber
  • 11
  • 2

2 Answers2

1

In JavaScript, there are objects. There's no associative array type. There are arrays with a length and numeric property names, but they're also objects. JSON is a serialization format and doesn't have bearing on JavaScript values. The syntax

var something = {
  propertyName: 0,
  // ...
};

is an object initializer, and the result is an object. There are other ways of creating objects too, using constructor functions and as of a few years ago with the class mechanisms. Still, for the most part, they're just objects.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

JavaScript is amazing, it's very cool you are learning it.

JavaScript isn't statically-typed. In this case, paymentRequest is a variable object. Although it looks like JSON, you are only attributing some properties with values. In this case, the currencyCode propery is set to 'USD'. You can add more properties, which can pretty much be anything, including references to itself or other objects, values or functions.

If you want to learn more I can suggest:

  1. The Net Ninja, he is amazing at teaching code.
  2. The Mozilla JavaScipt Reference, its a little bit more technical but a great resource once you get a hang of it.
  3. Once you get into really technical stuff, I recomend checking the official ECMA docs, like ECMA2021. But dont forget to check the backwards compatibility.

Blockquote