473,406 Members | 2,549 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,406 software developers and data experts.

JavaScript inheritance: How to call my parent method?

Consider example:

Animal = function(age) {
this.age = age;
};

Animal.prototype.sleep = function() {
alert("Animal Sleeping...");
};

Human = function(name, age) {
this.name = name;
this.age = age;
};

Human.prototype = new Animal();

Human.prototype.sleep = function() {
// How to call my parent sleep();
};

var h = new Human("Peter", 15);
h.sleep();

I want inside the sleep() method of Human class, call to its parent
sleep() method.
Any idea?

Thanks.
Jun 27 '08 #1
6 14853
howa wrote:
[snip]
>

Any idea?

Try googling "javascript inheritance"
Jun 27 '08 #2
On Jun 11, 5:17 pm, howa wrote:
Consider example:

Animal = function(age) {
this.age = age;

};
Why no declaration for the - Animal - variable, and why assign a
function expression here when a function declaration would be the more
natural means of getting the constructor into existance?
Animal.prototype.sleep = function() {
alert("Animal Sleeping...");

};

Human = function(name, age) {
this.name = name;
this.age = age;

};

Human.prototype = new Animal();

Human.prototype.sleep = function() {
// How to call my parent sleep();

};

var h = new Human("Peter", 15);
h.sleep();

I want inside the sleep() method of Human class, call to
its parent sleep() method.

Any idea?
If you must then:-

Human.prototype.sleep = function() {
Animal.prototype.sleep.call(this);
};

- will do what you ask, but you should be able to design that desire
out of system.

Richard.
Jun 27 '08 #3
Henry <rc*******@raindrop.co.ukwrites:
If you must then:-

Human.prototype.sleep = function() {
Animal.prototype.sleep.call(this);
};

- will do what you ask, but you should be able to design that desire
out of system.
Agreed, though there are some situations where this is more or less
the only sane solution and trying to design it away won't make things
better. Inheritance already couples the objects pretty rigidly, so one
more reference to the "super" object won't hurt much.

J.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #4
howa <ho******@gmail.comwrites:
Consider example:

Animal = function(age) {
this.age = age;
};

Animal.prototype.sleep = function() {
alert("Animal Sleeping...");
};

Human = function(name, age) {
this.name = name;
this.age = age;
};

Human.prototype = new Animal();
Ok, considering this example, you say that all humans inherit
from the same animal. Not from the "class" of objects created
by the Animal constructor, but from a specific object.

That's just wrong.

The animal used as prototype for humans doesn't have an age,
like other animals (or rather, it does have an "age" property,
but with the "undefined" value). If it did have an age, a proper
animal, then all humans would inherit that same date.
Inheritance in Javascript is between objects, not between
constructors (and not between classes, since there are none).
You appear to be trying to do class based inheritance in
Javascript. While probably possible to emulate, it won't
be easy, nor natural.
Human.prototype.sleep = function() {
// How to call my parent sleep();
Others have given examples. Another extreme example would be:

Human.prototype.sleep = function() {
delete this.sleep;
this.sleep();
this.sleep = arguments.callee;
}

Again, it's not something you want to do. Rather do something like:

Human.prototype.sleep = (function(){
var parentSleep = Human.prototype.sleep; // original value
return function() {
// something
parentSleep.call(this);
}
})();
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 27 '08 #5

Howa,

howa <ho******@gmail.comwrites:
Consider example:

Animal = function(age) {
this.age = age;
};

Animal.prototype.sleep = function() {
alert("Animal Sleeping...");
};

Human = function(name, age) {
this.name = name;
this.age = age;
};

Human.prototype = new Animal();

Human.prototype.sleep = function() {
// How to call my parent sleep();
};

var h = new Human("Peter", 15);
h.sleep();

I want inside the sleep() method of Human class, call to its parent
sleep() method.

Using the YAHOO!'s YUI class-based-inheritance-emulating functions, you
could do (untested)

Animal = function (a) {
...
}

Animal.prototype.sleep = function (t) {

...
}
Human = function (a) {

Animal.apply (this, arguments);
}
YAHOO.extend (Human, Animal);
Human.prototype.sleep = function (t) {

... // do some stuff

Human.superclass.sleep.call (this, t);

... // do some more stuff
}
-----

HTH,
Arnaud


>

Any idea?

Thanks.
Jun 27 '08 #6
On Jun 13, 8:41 am,
a...@remove.this.and.keep.what.follows.ionicsoft.c om (Arnaud Diederen
(aundro)) wrote:
Howa,

howa <howac...@gmail.comwrites:
Consider example:
Animal = function(age) {
this.age = age;
};
Animal.prototype.sleep = function() {
alert("Animal Sleeping...");
};
Human = function(name, age) {
this.name = name;
this.age = age;
};
Human.prototype = new Animal();
Human.prototype.sleep = function() {
// How to call my parent sleep();
};
var h = new Human("Peter", 15);
h.sleep();
I want inside the sleep() method of Human class, call to its parent
sleep() method.
I would do just exactly that - explicitly:-

sleep : function(t) {
Animal.prototoype.sleep.call(this, t);
}

>
Using the YAHOO!'s YUI class-based-inheritance-emulating functions, you
could do (untested)
The OP is interested in understanding how to solve the problem.

The function itself, with my comments interspersed:-

/**
* Utility to set up the prototype, constructor and superclass
properties to
* support an inheritance strategy that can chain constructors and
methods.
* Static members will not be inherited.
*
* @method extend
* @static
* @param {Function} subc the object to modify
* @param {Function} superc the object to inherit
* @param {Object} overrides additional properties/methods to add
to the
* subclass prototype. These will
override the
* matching items obtained from the
superclass
* if present.
*/
extend: function(subc, superc, overrides) {
if (!superc||!subc) {

// GS: Safe to omit - new - keyword.
throw new Error("extend failed, please check that " +
"all dependencies are included.");
}
var F = function() {};
F.prototype=superc.prototype;
subc.prototype=new F();
subc.prototype.constructor=subc;
subc.superclass=superc.prototype;

// GS: Consider using === (may result in increased performance in
JScript).
if (superc.prototype.constructor ==
Object.prototype.constructor) {
superc.prototype.constructor=superc;
}

if (overrides) {
for (var i in overrides) {

// GS: Important call to hasOwnProperty.
if (L.hasOwnProperty(overrides, i)) {
subc.prototype[i]=overrides[i];
}
}

L._IEEnumFix(subc.prototype, overrides);
}
},

Another possibility is to cache the function being new'd. One possible
way to accomplish this is to use the one the Activation object is in -
arguments.callee:-

var f = arguments.callee;

But this requires a check in the - extend - function, which gets
recursed.

if(arguments.length === 0) return;

It's good to see that YAHOO has finally fixed some of the significant
bugs in this code.

Animal = function (a) {
...

}

Animal.prototype.sleep = function (t) {

...

}

Human = function (a) {

Animal.apply (this, arguments);

}

YAHOO.extend (Human, Animal);

Human.prototype.sleep = function (t) {

... // do some stuff

Human.superclass.sleep.call (this, t);
Did you mean:

Human.superclass.prototype.sleep.call(this, t)

- ?

Or more explicit:-

Animal.prototype.sleep.call(this, t);
Garrett

>
HTH,
Arnaud

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: RR | last post by:
I have two classes: class Base { function Insert() { // does stuff } }
2
by: lkrubner | last post by:
My code was dying on the line below where I use method_exists: if (class_exists($nameOfClassToBeUsed)) { $object = new $nameOfClassToBeUsed(); $this->arrayOfAllTheObjectsSoFarLoaded = &...
9
by: keith | last post by:
I created a class libery which has name space Assembly and class Assembly and compiled it. Then created a C# project and called a method in the external class e.g. Assembly dll;...
4
by: Hadi | last post by:
Hello, Say I have three classes inheritance A <-- B <-- C And in A I have the Create method: class A { public override bool Create() {
33
by: Partha Protim Roy | last post by:
Hello, I have a Customer form say A to enter/update customer details. In the Form A I have a button which opens another form say B. In the Form B, I am providing user with a option to search...
2
by: engwar1 | last post by:
I have a page that my users will go to to upload files to my website. As I want to reuse the file upload code on multiple pages I put the file upload textbox/buttons on a user control which I plan...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: Steve Hershoff | last post by:
Hi everyone, We have a javascript function we'd like to call from within a C# method in our code-behind file. The way it has worked historically is we'd call the method from a hyperlink, like...
7
gskoli
by: gskoli | last post by:
Dear all, Let me tell you the scenario , i have called javascript function on radio button selection , Ex. Suppose There are 3 Radio Button . Let us consider i have clicked on one radio...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.