Domanda

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

È stato utile?

Soluzione

  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.

Altri suggerimenti

  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/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top