2
var a = function(){ this.sayFoo = function(){ console.log('foo'); }; } var b = function(){ console.log(this.prototype); //undefined this.sayBar = function(){ console.log('bar'); }; } b.prototype = new a(); var bInst = new b(); bInst.sayFoo(); bInst.sayBar(); console.log(b.prototype); //a {sayFoo: function} 

http://jsfiddle.net/KbBny/1/

How do I add sayBar to the b prototype inside the function constructor?

Does b.prototype = new a(); overwrite the prototype, or merge b's with a's?

3
  • 1
    The whole idea of a prototype is that you define it outside of the constructor, rather than inside. Commented May 16, 2013 at 8:55
  • Read this: stackoverflow.com/a/8096017/783743 Commented May 16, 2013 at 8:57
  • I can totally understand the wish to set the property inside the constructor, because setting it outside looks so disconnected. Added my own wrapperbased solution further down. Commented Dec 2, 2018 at 3:15

3 Answers 3

2

You are not using the correct inheritance pattern.

Use:

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

In your case you are performing a simple override, you are not correctly establishing inheritance. Object.create is ES5, but you could polyfill with this:

Object.create

if (!Object.create) { Object.create = function (o) { if (arguments.length > 1) { throw new Error('Object.create implementation only accepts the first parameter.'); } function F() {} F.prototype = o; return new F(); }; } 

Accessing the prototype

You can't access the prototype inside the definition block. You have a this reference for that.

var b = function() { a.call(this); b.prototype.doSomething = function() {console.log("b");}; }; b.prototype = Object.create(a.prototype); 

DEMO

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

5 Comments

Does this merge the prototypes? And how is the browser support for Object.create?
Thanks, would it be enough to include the source for Object.create in order to support it in older browsers? Or does it rely on other functions?
Ok, and the final question which I don't believe you have answered yet. How do I access the b prototype inside the function?
@alex23 Inside the b constructor function.
1

Does b.prototype = new a(); overwrite the prototype, or merge b's with a's?

It does overwrite it with a new a instance; nothing is merged (for example you'd need to update the b.prototype.constructor property). That's why you do add all properties to b.prototype after this line. However, actually you don't want to create an instance, but just set up the prototype chain correctly:

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

How do I add sayBar to the b prototype inside the function constructor?

You should not add it to the prototype, as it is not a prototype (shared) method - it's instance-specific to every a instance (at least it should be, otherwise you would put it on a.prototype and then it gets covered by above line). To get the instance method on all b instances as well, you use

var b = function(){ a.call(this); // invoke the `a` constructor on this instance }; 

10 Comments

Hmm, not sure if I'm following you on the last part. If I use this.sayBar = ... inside the b construcor function, it would have to be redefined for each instance of b, right? Wouldn't it be better to add it to the prototype?
@alex23 no, functions are not always in the prototype.
@alex23 Then please explain why I don' see two methods in the console: jsfiddle.net/KbBny/2
@alex23 no, a function added with this.func = function() { ... } is a direct property of that object, and doesn't touch the prototype at all.
@Johan: correctly. Your sayFoo and sayBar functions are currently redefined for each instance. Since they don't need access to any closure variables, they better should be on the prototype. And don't listen to alex23.
|
0

prototype Sort of inside constructor

You could use a wrapping function. I believe they are called decoratorfunctions in Javascript. Where you set the prototype. And then when you use that decoratorfunction as a constructor, you wont have to set the prototype separately. It will so to speak be set inside a function that acts as a constructor.

function Human(name, lastname, age) { function _human(name, lastname, age) { this.name = name; this.lastname = lastname; this.age = age; } _human.prototype.sayName = function() { console.log(this.name + " " + this.lastname); } var temp = new _human(name, lastname, age); return temp; } 

Then you just do:

var person = new Human("John", "Doe", 25); console.log(person); person.sayName(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.