473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Again i got stuck in prototype chaining?

dmjpro
2,476 2GB
Hey Experts!
I want to know the prototype chaining? How does it take part in Inheritance in JavaScript?
Mar 30 '09 #1
16 1942
Dormilich
8,658 Expert Mod 8TB
put in simple words, prototyping is the Javascript inheritance.

more info on google
Mar 30 '09 #2
dmjpro
2,476 2GB
I think it would be a best place to know how prototype chaining works ?
Mar 30 '09 #3
Dormilich
8,658 Expert Mod 8TB
you mean an example?
Mar 30 '09 #4
dmjpro
2,476 2GB
See ....
I have this example.
Expand|Select|Wrap|Line Numbers
  1. function Super(){
  2. this.a = 100;
  3. }
  4.  
  5. function Sub(){
  6. Super.apply(this,arguments);
  7. }
  8.  
  9. function test(){
  10. Sub.prototype = new Super();
  11. Super.prototype.x = 200;
  12. Sub s = new Sub();
  13. alert(s.a); alert(s.x);
  14. }
  15.  
Actually I got a sample code ...i changed here in two places.
There it was .... Sub.prototype.constructor = Sub and Super.prototype.constructor.apply..
Actually what would be the benifit if i left the code as it was?
Could you explain?
Mar 30 '09 #5
Dormilich
8,658 Expert Mod 8TB
@dmjpro
this re-sets the constructor function to Sub() (otherwise the Super() function would be called at: var obj = new Sub;)
@dmjpro
this calls Super's constructor function (which may not necessarily be named Super()) with the context of "this" set to your actual Sub object.

@dmjpro
probably less typing and less re-usability (though that may make hardly any difference in this case)
Mar 30 '09 #6
dmjpro
2,476 2GB
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 ;(
Mar 30 '09 #7
Dormilich
8,658 Expert Mod 8TB
@dmjpro
how do you know that?
@dmjpro
you know what the apply() function is for?
Mar 30 '09 #8
dmjpro
2,476 2GB
If i add something into Sub..

Expand|Select|Wrap|Line Numbers
  1. function Sub(){
  2. this.b = 200;
  3. .....
  4. }
  5.  
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 ? ;)
Mar 30 '09 #9
Dormilich
8,658 Expert Mod 8TB
@dmjpro
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
Mar 30 '09 #10
dmjpro
2,476 2GB
That means the properties of Super set into Sub.
Then what's the difference ... Super.prototype.constructor.apply and Super.apply? ;)
Mar 30 '09 #11
Dormilich
8,658 Expert Mod 8TB
in your case there isn't.
Mar 30 '09 #12
dmjpro
2,476 2GB
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 .. ;)
Mar 30 '09 #13
Dormilich
8,658 Expert Mod 8TB
@dmjpro
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
Expand|Select|Wrap|Line Numbers
  1. function Mammal(name){ 
  2.     this.name=name;
  3.     this.offspring=[];
  4. Mammal.prototype.haveABaby=function(){ 
  5.     var newBaby=new this.constructor("Baby "+this.name);
  6.     this.offspring.push(newBaby);
  7.     return newBaby;
  8. Mammal.prototype.toString=function(){ 
  9.     return '[Mammal "'+this.name+'"]';
  10.  
  11.  
  12. Cat.prototype = new Mammal();        // Here's where the inheritance occurs 
  13. /*
  14.   comment out this line and compare the output to the original one
  15. */
  16. Cat.prototype.constructor=Cat;       // Otherwise instances of Cat would have a constructor of Mammal 
  17. function Cat(name){ 
  18.     this.name=name;
  19. Cat.prototype.toString=function(){ 
  20.     return '[Cat "'+this.name+'"]';
  21.  
  22.  
  23. var myPet = new Cat('Felix');
  24. myPet.haveABaby();                    // calls a method inherited from Mammal 
  25.  
  26. alert(myPet.offspring[0]);            // results in '[Cat "Baby Felix"]' 
Mar 30 '09 #14
dmjpro
2,476 2GB
And what about Apply?
Give me an example where those two make differences? ;)
Mar 30 '09 #15
Dormilich
8,658 Expert Mod 8TB
@dmjpro
actually, you don't need Super.apply(), because the parent constructor is already called in Sub.prototype = new Super;
Mar 30 '09 #16
dmjpro
2,476 2GB
Cool Dude ;) It's really amazing.
Mar 31 '09 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
1
by: drnicwilliams | last post by:
The Prototype library gives us the $() operation for converting a DOM element id into the DOM element: $('element-id'). It also appends a bunch of functions to the resulting object. Sometimes...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
1
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
10
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
18
by: Aaron Gray | last post by:
I know this has probably been argued to death, but I am going to raise it fresh again, and basically lets have an unofficial 'isArray()' contest that we can hopefully put it to rest as best as we...
11
by: Patrick | last post by:
Trying this question again in a different way and expanding it to another newsgroup. Looking for how I would do this. For an html form; Say I have three check boxes A, B, and C . When I click...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.