Roli Agrawal
Roli Agrawal

Reputation: 2466

Adding function in object using Javascript prototype

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

Upvotes: 1

Views: 89

Answers (3)

FaXaq
FaXaq

Reputation: 123

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();

Upvotes: 0

Bergi
Bergi

Reputation: 664548

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.

Upvotes: 2

vijayP
vijayP

Reputation: 11502

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()

Upvotes: 0

Related Questions