1

I want to check if my javascript object is of a certain type.

I have an object (I cannot edit this object.) and I have made instances of that object. Now I want to loop through all the objects that are available at any given point and figure out which ones are the instances that I am looking for.

Example:

var myObject = function() { };
var obj_person = new myObject();

Now if I write

typeof obj_person

It gives me object which is not useful. I cannot use instanceof as it does not work in IE8.

Is there anyway to check if obj_person is an instance of myObject?

Thanks.

EDITED***

Here is the code

var arr_instances = [];
for(var v in window)
{
    var obj_required;
    if(typeof window[v] == 'object')
    {
        obj_required = window[v];
        if(obj_required.imgSrc)
        {
            arr_instances.push(obj_required);
        }
    }
}

Then I can iterate withing arr_instance and do whatever I want to do.

for(var i=0; i<arr_instances.len; i++)
{
    if(arr_instances[i].imgSrc == 'requiredImg.jpg')
    {
        arr_instances[i].imgSrc = 'newImg.jpg';
    }
}
sam
  • 11
  • 3
  • Are there any properties or functions unique to this type? – Brian S Feb 18 '14 at 16:25
  • A possible [duplicate](http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript) – linstantnoodles Feb 18 '14 at 16:27
  • No...just consider a situation like, I do not know what does this object look like. I just know that this object might be used in the page and want to figure out the instances. I just know the name 'myObject'. – sam Feb 18 '14 at 16:31
  • I am trying to loop through all the objects in a window. var scp = window; for(var v in scp) { num_counter++; if(typeof scp[v] == 'object') { num_tryCounter++; } In IE11, num_tryCounter is 845 whereas in IE8 num_tryCounter is 44. I am clueless. Please help. – sam Feb 18 '14 at 20:38
  • @Brian S - What if I have a unique property for this type? Would I be able to look for it? I need it to work on IE8. – sam Feb 19 '14 at 14:30
  • `if (obj_person.someProperty)` will return true if `someProperty` exists (and is non-falsey) – Brian S Feb 19 '14 at 14:35
  • @Brian S - Thanks for the comment. The worst problem is the concerned object is even not shown up in the iteration when i run the code in IE8 which makes it impossible for me to try anything. Do you have any suggestions for this? I have a code which works perfectly fine for all the modern browsers. However, I have to make it work on IE8 as well. – sam Feb 19 '14 at 14:52
  • Post some code, update your question – Brian S Feb 19 '14 at 14:54
  • @Brian S - Edited the original question with some more code. – sam Feb 19 '14 at 20:29
  • `obj_person instanceof myObject` returns `true` in IE8, as expected. Why do you think it does not work in this browser? –  Mar 05 '14 at 21:46

0 Answers0