/    Sign up×
Community /Pin to ProfileBookmark

Child Class Won’t Override Method (May be a bad approach…)

Hi all, in the following code I have a Parent and Child class. I’m trying to model a constructor/class type view. I inherit the parents methods by using “call”, but once this is done, any of the child methods that are named the same are overrided by the parent!

You can just copy and paste the below code to run it.

Any help would be greatly appreciated.

[code]
function ParentTest(el){

function initialize(obj, el){
obj.el = el;

alert(‘Valid initial…’);
}

this.publicMethod = function() {
alert(‘I am public!’);
privateMethod();
}

function privateMethod(){
alert(‘I am private!’);
}

this.overrideMe = function() {

alert(‘I am in ParentTest!’);
}

initialize(this, el);

}

function ChildTest(el, name){

var test = ‘private var’;

function initialize(obj, el, name){

ParentTest.call(obj, el);
obj.name = name;
obj.overrideMe();

}

this.showTest = function() {

alert(test);
}

this.show = function() {
alert(‘My el is: ‘ + this.el + ‘ and my name is: ‘ + this.name);
}

// This gets overrode by the parent.
this.overrideMe = function() {

alert(‘I am in ChildTest!’);
}

initialize(this, el, name);

}
var test= new ChildTest(‘el4’, ‘myName’);
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@zaxisauthorNov 21.2008 — I figured it out. I took out the ParentTest.call() in the constructor and after my ChildTest class just put:

ChildTest.prototype = new ParentTest();

Everything works as it should.

Thanks anyways ?
×

Success!

Help @zaxis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...