0

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?

2
  • 3
    Image object already exists in browser environment, you should change name of your object to another one, btw standard Image object already have width, height properties Commented Jan 12, 2013 at 3:10
  • Need to see the getNatural() function code, or at least know what it is returning Commented Jan 12, 2013 at 3:12

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

cool, thanks for that, makes obvious sense. @kirugan - thanks for the heads up on the Image object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.