1

I am newbie in Javascript i am trying to add some function in object prototype which is created by Object.prototype i tried this code

var a=function(){this.k="yes";}
a.prototype.b1=function(){console.log("function of a");}; 
var b=Object.create(a.prototype); 
b.prototype.c1=function(){console.log("function of b");};
b.c1();

Its giving me error 'Cannot set property 'c1' of undefined' I am not getting where i am doing mistake kindly guide me . Thanks in advance

3
  • 2
    is that the error you get? because I get that prototype doesnt exist (on b) also, you can't do var bObj = new b(), as b isnt a constructor. Commented Aug 21, 2015 at 8:36
  • 1
    @atmd yep, sry, didn't notice that b is not a constructor. Commented Aug 21, 2015 at 8:37
  • You forget the step where b is made a function that creates instances and has a .prototype Commented Aug 21, 2015 at 9:03

3 Answers 3

2

I'm not sure what exactly you were trying to do, but currently your problem is that b is a plain object (which inherits from a.prototype that has .b1 and .constructor properties) with no b.prototype property. Nonetheless you're trying to set an property on that non-existing thing.

You either were looking for

var a = {
    b1: function(){console.log("function of a");}
}; 
var b = Object.create(a); 
b.c1 = function(){console.log("function of b");};
b.c1();
b.b1();

with no constructor functions or .prototype properties involved - just plain prototype inheritance - or you were looking for

function A() { this.k="yes"; }
A.prototype.b1 = function(){console.log("function of A.prototype");};

function B() { A.call(this); }
B.prototype = Object.create(a.prototype); 
B.prototype.c1 = function(){console.log("function of B.prototype");};
var b = new B();
b.c1();
b.b1();

which is a typical example of inheritance between "class" structures, i.e constructors with accompanying prototype objects. You had forgotten to make B a function and instantiate it before calling a method.

Sign up to request clarification or add additional context in comments.

1 Comment

Great ... its really opened my eye
0

Your code should be like this:

var a=function(){this.k="yes";};
a.prototype.b1=function(){console.log("function of a");}; 
var b =function(){};
b.prototype=new a(); 
b.prototype.c1=function(){console.log("function of b");};
var bObj = new b();
bObj.c1()

1 Comment

0

You're trying to achieve two separated things here.

First :

var b=Object.create(a.prototype);

I assume that you're trying to extend a class in b. Consider modifying directly the b prototype after you created it :

//Create b class
var b = function(){this.key = 2};

//Extends a in b
var b.prototype = new a();

Second :

b.prototype.c1=function(){console.log("function of b");};
b.c1();

You're trying to call your function from your class with b.c1();. Try to instanciate it first in another variable var bObject = new b(); and then call the function assigned to te prototype : bObject.c1()

Your overall code should look like this :

//Create a class here
var a=function(){this.k="yes";};

//assign b1 function to a class
a.prototype.b1=function(){console.log("function of a");}; 

//Create b class
var b = function(){this.key = 2};

//extends a in b
b.prototype = new a();

//create c1 function in b class
b.prototype.c1=function(){console.log("function of b");};

//create bObj from b class
var bObj = new b();

//call c1 function defined in b class
bObj.c1();

//can also call b1 function from a class
bObj.b1();

3 Comments

No, it should not look like b.prototype = new a();
Btw, you have forgotten a b function, and your var b.prototype declaration is invalid as well.
Thanks @Bergi for the link. I'm learning something here. I'm editing for your second comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.