[英]Javascript function output | Constructor typeof
could someone please explain why this function is returning the value as true?有人可以解释为什么这个函数返回的值为真吗?
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Object</h2>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);
function isArray(myArray) {
return myArray.constructor === Array;
}
</script>
</body>
</html>
它返回 true,因为数组是使用Array
构造函数构造的,即使您使用数组字面量语法也是如此。
myArray.constructor
is equal to Array
because .constructor
property has a reference to the same value - Array
. myArray.constructor
等于Array
因为.constructor
属性引用了相同的值 - Array
。 Why?为什么? Because it was constructed by constructor function Array
.因为它是由构造函数Array
构造的。 Array === Array
is true Array === Array
为真
myArray.constructor === Array;
Both have type ' function ' so it will always turn out to be true .两者都有类型 ' function ',所以它总是会变成true 。
lets take this code.让我们拿这个代码。
let a=2;
console.log(a === Array);
this will log false because here we compare a number type with function type.这将记录错误,因为这里我们将数字类型与函数类型进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.