Again i got stuck in prototype chaining?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Again i got stuck in prototype chaining?

    Hey Experts!
    I want to know the prototype chaining? How does it take part in Inheritance in JavaScript?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    put in simple words, prototyping is the Javascript inheritance.

    more info on google

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      I think it would be a best place to know how prototype chaining works ?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you mean an example?

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          See ....
          I have this example.
          Code:
          function Super(){
          this.a = 100;
          }
          
          function Sub(){
          Super.apply(this,arguments);
          }
          
          function test(){
          Sub.prototype = new Super();
          Super.prototype.x = 200;
          Sub s = new Sub();
          alert(s.a); alert(s.x);
          }
          Actually I got a sample code ...i changed here in two places.
          There it was .... Sub.prototype.c onstructor = Sub and Super.prototype .constructor.ap ply..
          Actually what would be the benifit if i left the code as it was?
          Could you explain?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by dmjpro
            Actually I got a sample code ...i changed here in two places.
            There it was .... Sub.prototype.c onstructor = Sub
            this re-sets the constructor function to Sub() (otherwise the Super() function would be called at: var obj = new Sub;)
            Originally posted by dmjpro
            and Super.prototype .constructor.ap ply..
            this calls Super's constructor function (which may not necessarily be named Super()) with the context of "this" set to your actual Sub object.

            Originally posted by dmjpro
            Actually what would be the benifit if i left the code as it was?
            probably less typing and less re-usability (though that may make hardly any difference in this case)

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Still it's not clear.
              Still it's calling Sub function on calling new Sub()
              And what you told abut apply, i didn't get you ;(

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by dmjpro
                Still it's calling Sub function on calling new Sub()
                how do you know that?
                Originally posted by dmjpro
                And what you told abut apply, i didn't get you ;(
                you know what the apply() function is for?

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  If i add something into Sub..

                  Code:
                  function Sub(){
                  this.b = 200;
                  .....
                  }
                  If I alert s.b then it's coming. i am getting the Sub's properties as well as of Super.

                  Yeah i know .. actually what apply does here .. it simply copies the Super's properties and put it into Sub object. I don't know whether it is correct or not ? ;)

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by dmjpro
                    Yeah i know .. actually what apply does here .. it simply copies the Super's properties and put it into Sub object. I don't know whether it is correct or not ? ;)
                    nearly, apply() runs the specified function in a different context (= your own definition of "this" inside the called function) (it does not copy anything) i.e. here it creates the property "a" but the property is attached to the Sub object not the window object (window is the global scope/object that is used, if no context is specified (like if you would simply call Super() (without the new keyword)))

                    see apply() – MDC

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      That means the properties of Super set into Sub.
                      Then what's the difference ... Super.prototype .constructor.ap ply and Super.apply? ;)

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        in your case there isn't.

                        Comment

                        • dmjpro
                          Top Contributor
                          • Jan 2007
                          • 2476

                          #13
                          Then please give me an example ?
                          An one more thing .. where do i need to reset the constructor again ? Please give also an example of it .. ;)

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by dmjpro
                            An one more thing .. where do i need to reset the constructor again ?
                            you'll need the correct constructor function if you want to use inherited methods. It seems not a problem when creating instances of an object, rather than using them.

                            example taken from http://phrogz.net/JS/Classes/OOPinJS2.html
                            Code:
                            function Mammal(name){ 
                            	this.name=name;
                            	this.offspring=[];
                            } 
                            Mammal.prototype.haveABaby=function(){ 
                            	var newBaby=new [B]this.constructor[/B]("Baby "+this.name);
                            	this.offspring.push(newBaby);
                            	return newBaby;
                            } 
                            Mammal.prototype.toString=function(){ 
                            	return '[Mammal "'+this.name+'"]';
                            } 
                            
                            
                            Cat.prototype = new Mammal();        // Here's where the inheritance occurs 
                            /*
                              comment out this line and compare the output to the original one
                            */
                            [U]Cat.prototype.constructor=Cat;[/U]       // Otherwise instances of Cat would have a constructor of Mammal 
                            function Cat(name){ 
                            	this.name=name;
                            } 
                            Cat.prototype.toString=function(){ 
                            	return '[Cat "'+this.name+'"]';
                            } 
                            
                            
                            var myPet = new Cat('Felix');
                            myPet.haveABaby();                    // calls a method inherited from Mammal 
                            
                            alert(myPet.offspring[0]);            // results in '[Cat "Baby Felix"]'

                            Comment

                            • dmjpro
                              Top Contributor
                              • Jan 2007
                              • 2476

                              #15
                              And what about Apply?
                              Give me an example where those two make differences? ;)

                              Comment

                              Working...