Roger
Roger

Reputation: 1853

typeof object.constructor always returns a function. Is that true?

function Person(name){
 this.name = name;
}

p = new Person('John');
log(typeof p.constructor);

var f = {};
log(typeof f.constructor);

var f2 = new Object();
log(typeof f2.constructor);

All three log statements show 'function'.

Is there a case when the constructor of an object will NOT be 'function' ?

Upvotes: 3

Views: 406

Answers (2)

rahul
rahul

Reputation: 187030

An object constructor is merely a regular JavaScript function, so it's just as robust (ie: define parameters, call other functions etc). The difference between the two is that a constructor function is called via the new operator

Read

Object Constructor and prototyping

Upvotes: 0

Philippe Leybaert
Philippe Leybaert

Reputation: 171764

A constructor is a function in javascript, by definition. So the type will always be "function".

See: http://www.w3schools.com/jsref/jsref_constructor_math.asp

"The constructor property is a reference to the function that created an object."

The Mozilla documentation is even clearer:

Returns a reference to the Object function that created the instance's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name

Upvotes: 6

Related Questions