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:
I declare Function
object. This is no different from eg 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?
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
Correct - all of them have a prototype, although the prototype for each is different
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/
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.