0

If I have a function object:

function Calculator()
{
// some code
}

and then I instantiate with:

var calc=new Calculator();

Is it correct to say that my calc:

  1. is a Calculator object (and inherits from Object object)?
  2. inherits from Calculator object (and this in turn inherits from Object object)?

Thanks for clarifying this, cheers,

PSL
  • 123,204
  • 21
  • 253
  • 243
spirytus
  • 10,726
  • 14
  • 61
  • 75
  • Maybe this answer will shed some light on the matter: http://stackoverflow.com/a/16063711/1641941 or the following: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain As others suggested; it would be wise to get an understanding of how prototype works in JS (see first link) – HMR Dec 06 '13 at 00:30

4 Answers4

4

Every variable is an Object and thus has the methods and properties of an Object. However, technically there is no true inheritance in JavaScript since it is a prototypal language rather than classical. But, because everything is an object and everything is mutable, you can mimic inheritance by assigning properties of one object to another object. See jQuery's extend method or CoffeeScript's class extension.

But, to answer your question:

  1. Yes, calc is an instance of Calculator, meaning its prototype is Calculator.
  2. No, see above
jraede
  • 6,846
  • 5
  • 29
  • 32
  • Dart contains much more rigorous classes, and generally tries to fix the weaknesses of javascript as a language. https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#classes – jm0 Dec 06 '13 at 00:07
  • I actually like the fact that you can create your own "classes" on the fly with JavaScript. It can get you into trouble if you don't know what you're doing but if you're aware of the potential pitfalls then it's a pretty cool feature. – jraede Dec 06 '13 at 00:09
2

Hmmm... my understanding is that it is a Calculator object, but inheritance is not really a fair term.

It is an instance of the Calculator object, which is in turn an instance of object. But since they are not classes as one would traditionally imagine, the term "inherit" is not technically correct.

jm0
  • 3,294
  • 2
  • 16
  • 18
0

All objects in JavaScript "inherit" from the type Object. I put the word inherit in quotes because it's not classical inheritance, but prototypical inheritance because JavaScript is a prototype based language.

Read more about it here (Seriously, because this won't make complete sense unless you understand prototypes).

Now, the simple answer to your question is that new Calculator() creates an instance of Calculator which inherits from Object. So number 1 is little closer. Number 2 is wrong because the calculator object doesn't inherit from itself. It's an is-a relationship.

Now there's actually one more piece: the function type. In reality, our "inheritance" in classical terms looks like this:

Calculator <- Function <- Object (<- stands for inherits from)

Community
  • 1
  • 1
linstantnoodles
  • 4,350
  • 1
  • 22
  • 24
0

There is no idea of inheritance per say in JavaScript. Your Calculator thing is basically an object and it has a property called prototype, which is used as, well, a prototype for all instances of the Calculator object.

basically calculator = new Calculator will be an instance of Calculator, but internally all it's properties (until you explicitly change them) will point to Calculator.prototype. In other words, a new calculator is pretty much the same object as Calculator.prototype but with a different object-id inside of the JavaScript interpreter.

So, in the end calculator is an instance of Calculator and eventually Object, but it does not 'inherit' from Calculator, coz there is no inheritance, there are the object prototypes which can be instances of anything.

Nikolay
  • 1,392
  • 1
  • 9
  • 15