JavaScript - 数组 constructor 构造函数属性
在 JavaScript 中,数组构造函数属性用于返回数组的构造函数。对于 JavaScript 数组,constructor 属性返回:function Array() { [原生代码] }。
数组构造函数的返回值是函数的引用值,而不是函数的名称。
语法
以下是 JavaScript 数组构造函数的语法 -
array.constructor
这里,array 是一个数组。
返回值
它返回指定数组的构造函数。
示例
示例 1
在下面的示例中,我们使用 JavaScript 数组的构造函数属性返回"animals"数组的构造函数 -
<html>
<body>
<script>
let animals = ["lion", "cheetah", "tiger", "elephant"];
let result = animals.constructor;
document.write(result);
</script>
</body>
</html>
输出
执行上述程序,返回结果如下:
function Array() { [native code] }
示例 2
注意 - Array() 可以使用或不使用 new 调用。两者都会创建一个新的 Array 实例。
在本例中,我们使用"new"关键字调用 Array()。然后,我们使用 Array 的构造函数属性返回数组的构造函数 -
<html>
<body>
<script>
let animals = new Array ("lion", "cheetah", "tiger", "elephant");
let result = animals.constructor;
document.write(result);
</script>
</body>
</html>
输出
由于带有 new 关键字的 Array() 创建了一个新的 Array 实例,因此 Array 构造函数属性返回的结果如下:
function Array() { [native code] }
示例 3
这里,我们调用了不带"new"关键字的 Array()。然后我们在其上使用 Array 构造函数属性 -
<html>
<body>
<script>
let animals = Array ("lion", "cheetah", "tiger", "elephant");
let result = animals.constructor;
document.write(result);
</script>
</body>
</html>
输出
由于不带 new 关键字的 Array() 也会创建一个新的 Array 实例,因此 Array 构造函数属性返回结果如下:
function Array() { [native code] }

