Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
JavaScript
function FooParent (a){
    this.prop = a;
    this.val = a.value;//*
}
var elem = new FooParent(document.getElementById("someDomElement");
alert(elem.val);
//alerts the value of the form element no prob!

If I then add this block to the same code:
JavaScript
function FooKid (a, b) {
    FooParent.call(this, b);
    this.kidProp = b;
}
FooKid.prototype = new FooParent();
FooKid.prototype.constructor = FooKid;
var elem2 = new FooKid(document.getElementById("someDomElement");
alert(elem2.val);
//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, and this.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
Comments
Mohibur Rashid 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....

inheritance in javascript:
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);
 
Share this answer
 
In you code FooParent expects a param 'a'
In your line...
JavaScript
new FooParent();

...you haven't provided this param.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900