SuperNinja
SuperNinja

Reputation: 1596

Updating Javascript Object

First of all, javascript newb here and working on a little pet project to try to get better at this. I am building an Image object

function Image (id, name, height, width) {
    this.id     = id;
    this.name   = name;
    this.height = height;
    this.width  = width;
}

The first order of business is writing the id and name which I can do.

var image = new Image(
    json.revision_images[i].id, 
    json.revision_images[i].OrgImageName,
    json.revision_images[i].galleryid
);

My next step is adding the height and width to each instatiation of the object, this is where I have a problem wrapping my head around the object concept.

I have a little function that is getting the natural height and width of the referencing thumbnail.

function checkImageDimensions (id) 
{
     var img = $("#" + id);
     naturalDimension = getNatural(img);
}

How do I go about assigning

this.height = naturalDimension.oHeight;
this.width = naturalDimension.oWidth;

to the object based on id?

Upvotes: 0

Views: 72

Answers (1)

Felix Kling
Felix Kling

Reputation: 816272

As kirugan said, Image is already an existing function in browser. Better rename yours. Apart from that:

Store your image objects in a map, keyed by ID:

var images = {};
// somewhere
images[json.revision_images[i].id] = new MyImage(
   json.revision_images[i].id, 
   json.revision_images[i].OrgImageName,
   json.revision_images[i].galleryid
);
// and
function checkImageDimensions (id) {
   var naturalDimension = getNatural($("#" + id));
   images[id].width = naturalDimension.width;
   images[id].height = naturalDimension.height;
}

Upvotes: 1

Related Questions