3

I am using inheritance in javascript. I am stuck in a situation where i have to check whether the First object inherited in the Second object. Example :

   function Parent(name)
   {
     var self = this;

     self.Name = name;

     self.Check = function() {
       for(var i = 0; i < ChildCollection.length ;i++)
       {
          //here i want to check whether self is the object which is
          //inherited in ChildCollection[i] 

          alert(true or false);
       }
     }
   }

   function Child(name)
   {
     var self = this;

     Child.prototype.constructor = Child;   

     self.Name = name;
   }

   $(function() {       

      var ChildCollection = new Array()

      for(var i = 1; i <= 2 ;i++)
      {
         Child.prototype = new Parent("TestParent_" + i);              
         var child = new Child("TestChild_" + i);
         ChildCollection.push(child);
      }             

       ChildCollection[1].Check();
    });  

In the above code i have created 2 classes Parent and Child. Child inherit Parent. I have create a global ChildCollection Array which contains all child objects. In parent class there is a check function which i want to create, it should loop through the ChildCollection array and check whether the current object means (self or this) is inherited or a part of current looped child object.

To make very clear i called check on ChildCollection's second object i.e. ChildCollection[1].Check(). If i am clear or not wrong than first alert should be false and second alert should be true.

Please guid me to solve this issue and sorry if i am on totally on wrong track and please explain me what i am doing wrong ?

1 Answer 1

0

In the above code i have created 2 classes Parent and Child. Child inherit Parent.

The way you're doing that is very, very unusual. You're giving Child a new and different prototype just before every time you use it. That's probably not what you want to do. This other answer here on Stack Overflow provides an example of the "usual" way of doing inheritance in JavaScript. I've also written a helper script called Lineage to reduce the typing involved.

I have create a global ChildCollection Array which contains all child objects.

It's not a global, it's a variable within your ready callback (the function you're passing into $()). It's not accessible from Parent where you've defined Parent, and so this line:

for(var i = 0; i < ChildCollection.length ;i++)

...will fail with a ReferenceError.

You can fix that in one of three ways:

  1. Passing ChildCollection into the Parent contructor as an argument.

  2. Making ChildCollection actually global (which I would avoid).

  3. Moving your Parent and Child functions into your ready callback so they have access to it.

And actually, to avoid creating globals (Parent and Child) but avoid having Parent rely on a pseudo-global, you can combine #1 and #3.

Re the code comment:

here i want to check whether self is the object which is inherited in ChildCollection[i]

I don't know what you mean by "inherited" in that sentence, but if you want to know whether self is in ChildCollection:

var found = false;
for(var i = 0; !found && i < ChildCollection.length; i++) {
    if (ChildCollection[i] === self) {
        found = true;
    }
}
alert(found);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.