2

There is a function hi in the base class. There is a property name in the sub class.

function Base() {}
Base.prototype.hi = function () {
    console.log("hi " + this.name);
}

function Sub(name) {
    this.name = name;
}
Sub.prototype = new Base();

mySub = new Sub("rohita");
mySub.hi();

The output is

hi rohita

How is this from base class able to access name property from sub class in the hi function?

Does this not go against oops fundamentals?

Kiran
  • 388
  • 2
  • 16
  • 1
    JavaScript is not a pure OOP language. – DigiFriend May 02 '17 at 13:35
  • 2
    `Sub.prototype = new Base();` is also not how its done. `Object.create( Base.prototype )` to extend correctly. If you do that, however, this should easily work. – somethinghere May 02 '17 at 13:36
  • this is giving the same output as the one given in the question. According to http://stackoverflow.com/a/17952160/3304576, the difference is calling of the constructor. That should not have an effect on my function right? – Kiran May 02 '17 at 13:41
  • @Kiran Yeah sure, but do it right. Mostly because this seems like it might be more confusing, since javascript _is not_ OOP. It allows for those patterns, but is not made for it directly. – somethinghere May 02 '17 at 13:42
  • 4
    @DigiFriend: Kiran is having a very severe misunderstanding of OOP. This example would work exactly the same in Java, C++, C# and Go. Methods inherited from parent class are "inherited" and thus have access to the `this` of the current class. They are not called upon the base class but by the child class. Indeed, even saying that methods are called by a class is an extreme misunderstanding of OOP. Methods are called by objects, not classes. – slebetman May 02 '17 at 13:47
  • @slebetman Given that `Base` does not have a `name` member (that could be overwritten by subclasses) at all, I doubt this would work in the languages you mentioned. – Bergi May 02 '17 at 14:36
  • @slebetman that's cleared a lot of things. Thank you! I found this which describes exactly what you said https://youtu.be/PMfcsYzj-9M at 7:00 – Kiran May 05 '17 at 02:41

1 Answers1

0

You're misunderstanding the example you're representing. All instances of Sub class get the name property, on the contrary no Base class instance gets access to the name property.

Have a careful look:

mySub = new Sub("rohita");
mySub.hi();
// since Sub class doesn't override the hi method, it falls back to the parent's one,
// thus this.name is valid for any instances of Sub class.. not of Base class,
// Base class instances doesn't really access the name property of Sub class..
// to prove this let's log out `this.name` for any instance of Base class,
// it'll be simply `undefined`, but for the Sub class, it's the one already defined by Sub class itself

myBase = new Base();
myBase.hi(); // => hello undefined // makes sense now, right?

How is this from base class able to access name property from sub class in the hi function?

this from Base class doesn't really access the property of Sub class, this.name is clearly undefined from Base class in other word, any instance of Base class.

Since Sub class doesn't override the hi method inherited from Base class, invoking hi on Sub instance falls back to the parent one, in that context this clearly refers the Sub class, hence the name property of it.

some user
  • 1,693
  • 2
  • 14
  • 31