constructor 属性 (Object)

指定创建一个对象的函数。

语法

object.constructor

备注

必需的object是一个对象或函数的名称。

constructor属性是每个具有原型的对象的原型成员。这包括除GlobalMath对象之外的所有内部JavaScript对象。constructor属性包含了对某种函数的引用,此种函数构造了特定对象的实例。

示例

下面的示例演示了constructor属性的用法。

// A constructor function.
function MyObj() {
  this.number = 1;
}
var x = new String("Hi");
if (x.constructor == String)
  document.write("Object is a String.");
document.write ("<br />");
var y = new MyObj;
if (y.constructor == MyObj)
  document.write("Object constructor is MyObj.");
// Output:
// Object is a String.
// Object constructor is MyObj.

必备条件

在以下文档模式中受支持:怪异模式、Internet Explorer 6标准模式、Internet Explorer 7标准模式、Internet Explorer 8标准模式、Internet Explorer 9标准模式、Internet Explorer 10标准模式、Internet Explorer 11标准模式。应用商店应用(Windows 8和Windows Phone 8.1)中也受支持。请参阅版本信息

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。