totally
totally

Reputation: 413

Object vs function constructor in JavaScript

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

Upvotes: 1

Views: 168

Answers (2)

Evan Davis
Evan Davis

Reputation: 36592

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'

Upvotes: 1

georg
georg

Reputation: 214969

Ok, to sum it up:

  • every object has a hidden __proto__ property
  • functions are objects that also have a prototype property
  • 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:

String object: member of the Object type that is an instance of the standard built-in String constructor

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.

Upvotes: 2

Related Questions