IAmYourFaja
IAmYourFaja

Reputation: 56894

What kinds of objects is this Javascript creating?

I am scrambling to fix a production error and am not a JS developer. Before my analysis can go any further I need to be absolutely confident that I am making some correct assumptions about the following line of code:

var iVO = {
    "images":{}
};
var thisImage = $(this).data("data");

iVO["images"][thisImage.fileKey] = thisImage;
iVO["images"][thisImage.imageType] = imageType;
iVO["images"][thisImage.uploadReason] = uploadReason;

Here are my assumptions. If any are correct or misguided, please correct me:

The thing that I don't get is the significance of the "images" index, What is the value/meaning of iVO["images"]? What does information/objects this first/outer array represent?

Upvotes: 0

Views: 53

Answers (2)

hvgotcodes
hvgotcodes

Reputation: 120168

iVO is an object literal. It functions like a hash.

iVO["images"] looks like array access, but in this case the code is accessing the images property on iVO. In the end, it returns what images points at, which is {}, another object literal.

var thisImage = $(this).data("data") is using a jquery function. The documentation for jQuery.data is here. thisImage is a reference to the data returned from the invocation of data

iVO["images"][thisImage.fileKey] = thisImage;

is just setting a value. The first access is accessing the images property on iVO, which was a an object literal. From that object literal, the code assigns the property thisImage.fileKey (whatever that is, it comes out of the data call on the previous line) the value of thisImage.

So, when you say

iVO is an array of JSON objects

iVO is a single object literal, that contains another object literal under the images property.

That object literal has 3 values stuffed in it. The keys (the named of the properties) depends on what the data call returned. The values depend on thisImage (the result of the data call), and the variables imageType, and uploadReason.

You can use your debugger to step thru this code and see what the values are at each step.

Note JSON is not coming into play here. From json.org, "JSON is a text format that is completely language independent...."

Upvotes: 3

James Montagne
James Montagne

Reputation: 78630

iVO is an object. This object has a property named images which is itself also an object. This object has 3 properties, the names of which are contained in thisImage, the contents of which I cannot determine based on the code posted.

To answer your last question, iVO["images"] is equivalent to iVO.images. It is the images property of iVO.

Upvotes: 1

Related Questions