Click here to Skip to main content
11,932,048 members (55,124 online)
Rate this:
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 15-Dec-12 0:30am
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....
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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.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