I want to clear up a concept. Please tell me if my understand is correct:
Many javascript build-in objects, like Object, String, Array, Date, XMLHttpRequest, we keep saying they are objects, but they are actually constructor functions, am I right?
or these two name are used interchangeably.
Thanks
2 Answers 2
Ok, to sum it up:
- every object has a hidden
__proto__property - functions are objects that also have a
prototypeproperty - if, for some object O and function F,
O.__proto__ == F.prototype, we say that "O is an instance of F" - "F object" is a way to refer to an object that is an instance of "F". For example:
and the same for other built-in and user-defined types. If you have
function Point(x,y) { ... }
p = new Point(10,20)
then "p is a Point object". In a casual conversation you're also allowed to say "p is a Point" although this isn't strictly correct.
1 Comment
Object's prototype is the root prototype for most entities in JavaScript.
The items you list are all constructor functions, yes.
typeof Array // 'function'
Invoking a constructor returns an object.
typeof (new Array()) // 'object'
typeof (new Date()) // 'object'
4 Comments
Object.prototype (which inherits from null) is the root prototype, not the constructor Object itself.Function.__prototype__ === Function.prototype and Function.prototype.__prototype__===Object.prototype).
objectdoesn't really mean anything. Almost everything is an "object". Now these elements are constructors indeed, which are functions, and contains static methods such asArray.isArray