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

Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
  • 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. – atmd Aug 21 '15 at 08:36
  • 1
    @atmd yep, sry, didn't notice that b is not a constructor. – fuyushimoya Aug 21 '15 at 08:37
  • You forget the step where `b` is made a function that creates instances and has a `.prototype`… – Bergi Aug 21 '15 at 09:03

3 Answers3

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
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()
vijayP
  • 11,432
  • 5
  • 25
  • 40
  • [Not exactly, no](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) – Bergi Aug 21 '15 at 09:06
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();
FaXaq
  • 123
  • 1
  • 7
  • No, [it should *not*](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) look like `b.prototype = new a();` – Bergi Aug 21 '15 at 09:04
  • Btw, you have forgotten a `b` function, and your `var b.prototype` declaration is invalid as well. – Bergi Aug 21 '15 at 09:06
  • Thanks @Bergi for the link. I'm learning something here. I'm editing for your second comment – FaXaq Aug 21 '15 at 09:32