![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
| Any idea? |
#3
| |||
| |||
|
|
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? |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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(); |
#6
| |||
| |||
|
|
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. |
#7
| ||||
| ||||
|
|
Howa, howa <howac... (AT) gmail (DOT) com> writes: 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) The OP is interested in understanding how to solve the problem. |
|
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); |
|
HTH, Arnaud |
![]() |
| Thread Tools | |
| Display Modes | |
| |