Hey Experts!
I want to know the prototype chaining? How does it take part in Inheritance in JavaScript?
16 1942
put in simple words, prototyping is the Javascript inheritance.
more info on google
I think it would be a best place to know how prototype chaining works ?
See ....
I have this example. -
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.constructor = Sub and Super.prototype.constructor.apply..
Actually what would be the benifit if i left the code as it was?
Could you explain?
@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)
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 ;(
@dmjpro
how do you know that? @dmjpro
you know what the apply() function is for?
If i add something into Sub.. -
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 ? ;)
@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
That means the properties of Super set into Sub.
Then what's the difference ... Super.prototype.constructor.apply and Super.apply? ;)
in your case there isn't.
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 .. ;)
@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 - function Mammal(name){
-
this.name=name;
-
this.offspring=[];
-
}
-
Mammal.prototype.haveABaby=function(){
-
var newBaby=new this.constructor("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
-
*/
- Cat.prototype.constructor=Cat; // 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"]'
And what about Apply?
Give me an example where those two make differences? ;)
@dmjpro
actually, you don't need Super.apply(), because the parent constructor is already called in Sub.prototype = new Super;
Cool Dude ;) It's really amazing.
Sign in to post your reply or Sign up for a free account.
Similar topics
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)...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |