1

I'm trying to wrap my head around Javascript inheritance and wanted to check if my understanding is correct. When I declare Javascript function in JS like that:

function Animal() {
}

Now first questions:

  1. I declare Function object. This is no different from e.g. Array that JS comes with. Array is also Function object natively provided by JS is that right? And so is Object and RegExp and others? Is that right?

  2. Its incorrect to say that this object inherits from Function but can someone explain to me why? Is this because there is no formal class used as in Java and other languages? Or is it because Animal takes properties of actual existing object rather then sort of blueprint that class is in other languages? Is there any other reason?

Clarification on the above will be much appreciated, cheers,

EDIT: My question seems to create confusion so I should clarify. In point 1 I'm not asking what Array returns. I'm simply trying to confirm my understanding that the function Animal and Array both have in common that their [[Prototype]] property reference the same Function.prototype and so both are Function objects. If I understand correctly Animal is different from Array however cause Animal.prototype and Array.prototype is obviously different.

In point 2 I'm looking for explanation why its incorrect to say that Animal "inherits" from Function.

I hope this makes more sense, thank you all

spirytus
  • 10,726
  • 14
  • 61
  • 75
  • http://blog.javascriptroom.com/2013/01/14/objects-and-the-prototype-chain/ might be a useful article to read. – rlemon Dec 06 '13 at 21:10
  • It is an instance of a `Function`. It doesn't make sense for the language to instead make a new type of object for your function, as it probably have overhead and make less sense than being an instance anyways. – Waleed Khan Dec 06 '13 at 21:15

2 Answers2

0
  1. Correct - all of them have a prototype, although the prototype for each is different

  2. All functions are objects in Javascript (see below example)

Try these to confirm your beliefs...

alert(Function instanceof Object); // true
alert(Object instanceof Function); // true
alert(Array instanceof Object); // true
alert(Object instanceof Array); // false
alert(RegExp instanceof Function); // true
alert(Function instanceof RegExp); // false

This article helped me understand the javascript prototype, which is how inheritance works in Javascript.

http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/

Andy Jones
  • 6,205
  • 4
  • 31
  • 47
  • That's not what OP was asking imo. I think he was asking how come the result of calling `Array` for example is not a function - a good answer should explain that. – Benjamin Gruenbaum Dec 06 '13 at 21:21
  • I understand why calling new Array doesn't return a function, this is because Array.prototype is actually an object (Array.prototype.__proto__===Object.prototype). I updated my question now, I hope this will make it more clear – spirytus Dec 06 '13 at 22:05
0
  1. Yes, you cannot extend Array though so Array is a little different from your own functions.
  2. You could say objects inherrit from prototype up the prototype chain but you should not confuse it with inheritance as is in a class based language. More on prototype and constructor functions can be found here.

Note on 2: Instances created with the Animal constructor do not have Function up the prototype chain but the Animal constructor itself does have Function up it's prototype chain:

function Animal() {}
var a = new Animal();
console.log(a instanceof Function);//false
console.log(a instanceof Object);//true
console.log(Animal instanceof Function);//true
console.log(Animal instanceof Object);//true
var noProt = Object.create(null);
console.log(noProt instanceof Object);//false
console.log(noProt.hasOwnProperty);//undefined

All object instances (including Function) have Object in their prototype chain except when you use Object.create passing null as the first argument. To me this is like setting window.undefined to a value other than window.undefined as the assumption that every variable value in JS is an object goes out the window when someone does something like that.

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160
  • what you mean extend Array? if I go var a=new Array(); it will give me object based on Array.prototype. Also if I do var o={};o.__proto__=Array.prototype o becomes array. Could you explain what you mean please – spirytus Dec 07 '13 at 07:35
  • @spirytus Can't do this: (copy and paste) the constructor doesn't work `var MyArr=function(){ Array.apply(arguments); }; MyArr.prototype=Object.create(Array.prototype); var my=new MyArr(1,2,3); console.log(my.length);//=0 my.push(4); console.log(my);//=[4] my.push(5); console.log(my.length);//=2` – HMR Dec 07 '13 at 08:52