Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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' ?

like image 867
Roger Avatar asked Jan 28 '26 14:01

Roger


1 Answers

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

like image 142
Philippe Leybaert Avatar answered Jan 31 '26 04:01

Philippe Leybaert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!