Click here to Skip to main content
11,790,021 members (44,331 online)
Rate this: bad
Please Sign up or sign in to vote.
I am trying to create some objects and wrapper classes to simplify some form checking methods. I have researched the standard routine for JavaScript inheritance but I am getting a rather odd error when running my code. Here is the gist:
function FooParent (a){
    this.prop = a;
    this.val = a.value;//*
var elem = new FooParent(document.getElementById("someDomElement");
//alerts the value of the form element no prob!
If I then add this block to the same code:
function FooKid (a, b) {, b);
    this.kidProp = b;
FooKid.prototype = new FooParent();
FooKid.prototype.constructor = FooKid;
var elem2 = new FooKid(document.getElementById("someDomElement");
//the variable declaration in the last two lines is inconsequential, the code breaks on line 3 when I run it after adding the last block. 
The error that firebug gives me is that in the parent object this.val = a.value; is now undefined when it worked prior to trying to inherit from it. I also tried... this.val = this.prop.value, andthis.val = prop.value
(all should point to the same thing So in conclusion, my question is: why is this code breaking? Where is the bug?
Posted 14-Dec-12 23:30pm
Mohibur Rashid at 15-Dec-12 7:13am
You have to coding error
on line 6 of first part of code
on line 6 of second part of code

FooParent only receive one parameter but you are calling it with two parameter

and so on....
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

inheritance in javascript:
if (typeof Object.create !== 'function') {
	Object.create = function (o) {
		function F() { }
		F.prototype = o;
		return new F();
var newObject1 = Object.create({key1:'valuekke'});
var newObject2 = Object.create(newObject1);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In you code FooParent expects a param 'a'
In your line...
new FooParent(); haven't provided this param.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 1,029
1 Maciej Los 790
2 KrunalRohit 686
3 CPallini 606
4 Richard MacCutchan 460

Advertise | Privacy | Mobile
Web01 | 2.8.1509028.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100