tags:

views:

37

answers:

1

I want to settle this confusion in my mind once and for all! I am having trouble reaching some of my class properties and methods. Now, I know I could use a object literal and append all of my references with the objects name, e.i. animal., but I want to know how to handle this with an instantiated class.

Here is a quick example of my code..

function animal(){
       this.type = "Reptile";
       this.sayType = function(){
           //from my experiences "this" right here still refers to "animal"
           alert(this.type);
      };
      this.names = {
          name : "Lizard",
          sayTypeAndName : function(){
             //Now "this" refers to "names" not the "sayTypeAndName" method or the parent
             //animal. If I try to refer to "animal" like I would in an object literal 
             //and construct the class. I get an error saying the method below does 
             //not exist. HOW DO I CORRECTLY REFER TO THE PARENT CLASS PROPERTIES?
             //DO I STICK THE PARENT CLASS PROPERTIES AND METHODS INTO MY "names" object?
             animal.sayType(animal.type+" "+this.name);
          }
      };//end of names object
}//end of class

Thank you all for any help on this

+1  A: 
function SomeClass() {
    var self = this;
    this.something = {
        foo: function() {
            self; // <= points to instance of SomeClass
        };
    };
}
J-P
Nice! Thank you. Yeah, I did a "this" reference inside "something" but I didn't think to put a "this" reference at the "top level". THANK YOU!
srhyne
BTW your blog is tremendous! You were the first person that ever showed me how easy it was to add to the jQuery prototype without having to write a plugin. SOO USEFUL. Good stuff.
srhyne
Glad it helped :)
J-P