0

Please refer - http://jsfiddle.net/sf4oaxun/

function a() {
    this.say = function () {
        alert("hello");
    };
}

a.prototype.what = function () {
    alert("234234");
};

function b() {}

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

var b1 = new b();

b1.say();
  1. The 2 commented lines, are they different?
  2. Why does say not get invoked, and errors out when using either of the commented lines(please uncomment it)

    b.prototype = Object.create(a);
    b.prototype = a;
    
  3. Quick question added - If 'b' is inheriting from 'a' using b.prototype = Object.create(a.prototype), then would instances of b not be able to call any method of a which is not defined on a's prototype? Please refer - jsfiddle.net/sf4oaxun/3
  • Please have a look at [Benefits of using `Object.create` for inheritance](http://stackoverflow.com/q/17392857/218196). Both lines are actually incorrect. `say` doesn't get invoked because it does not exist on `b`. Why does it not exist? Well, when exactly is `this.say` created? – Felix Kling Feb 28 '15 at 06:14
  • I can only say again, that you should have a look at my answer if you haven't yet: http://stackoverflow.com/a/17393153/218196. It will hopefully answer your questions. – Felix Kling Feb 28 '15 at 06:46
  • good read. One quick question in that. http://jsfiddle.net/sf4oaxun/5/ does it mean "b" is inheriting from "a" – kushal bhandari Feb 28 '15 at 06:50

2 Answers2

1

say is only available within the function a's constructor. It will not be inherited.

so that's the reason why b1.say() was not available.

Another thing, both the statements are wrong.

It should be either

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

so that what property will be inherited by b.

Another way of doing is

b.prototype = a.prototype

But in this case, any changes to b.prototype will affect a.prototype

In case if you want to inherit the constructor properties as well, then

b.prototype = new a();

DEMO

mohamedrias
  • 18,326
  • 2
  • 38
  • 47
  • if you want to inherit the constructor properties, then use b.prototype = new a(); – mohamedrias Feb 28 '15 at 06:39
  • 1
    @mohamedrias: Better use `Object.create` and call `a.call(this);` inside `b`. – Felix Kling Feb 28 '15 at 06:44
  • @mohamedrias - Thanks, quick question. Please check - http://jsfiddle.net/sf4oaxun/5/. Does the code mean "b" inheriting from "a"? – kushal bhandari Feb 28 '15 at 06:45
  • yes, we are using a.call(this) to apply b's context to get the properties of a. But in your example, you're assigning name in a's constructor. So in that scenario, I would just go for creating new instance of a, because b function as such doesn't do anything important. check http://jsfiddle.net/sf4oaxun/6/ – mohamedrias Feb 28 '15 at 06:56
0

When you use Object.create the prototype is curried over, but your constructor is never run. This the code inside the original A "class" isn't actially executing. To get b1 to have both say and what, you would do

b.prototype = new a();

TomFuertes
  • 7,150
  • 5
  • 35
  • 49