简体   繁体   English

typeof object.constructor总是返回一个函数。真的吗?

[英]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' ? 是否存在对象的构造函数不是“函数”的情况?

A constructor is a function in javascript, by definition . 根据定义 ,构造函数是javascript中的函数。 So the type will always be "function". 所以类型永远是“功能”。

See: http://www.w3schools.com/jsref/jsref_constructor_math.asp 请参阅: 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: Mozilla文档更清晰:

Returns a reference to the Object function that created the instance's prototype. 返回对创建实例原型的Object函数的引用。 Note that the value of this property is a reference to the function itself, not a string containing the function's name 请注意,此属性的值是对函数本身的引用,而不是包含函数名称的字符串

An object constructor is merely a regular JavaScript function, so it's just as robust (ie: define parameters, call other functions etc). 对象构造函数只是一个常规的JavaScript函数,因此它也很健壮(即:定义参数,调用其他函数等)。 The difference between the two is that a constructor function is called via the new operator 两者之间的区别在于通过new运算符调用构造函数

Read

Object Constructor and prototyping 对象构造函数和原型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM