constructor 属性返回变量或对象的构造函数。
constructor 属性返回变量或对象的构造函数。
运行结果:
function String() { [native code] }function Number() { [native code] }function Boolean() { [native code] }function Array() { [native code] }function Object() { [native code] }function Date() { [native code] }function Function() { [native code] }
判断是否为日期,数组的例子
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>判断是否为日期、数组</title></head><body><p>判断是否为日期。</p>
<p id="demo1"></p><p>判断是否为数组。</p><p id="demo2"></p><script> var myDate = new Date(); var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = isDate(myDate); document.getElementById("demo2").innerHTML = isArray(fruits); function isDate(myDate) { return myDate.constructor.toString().indexOf("Date") > -1; } function isArray(myArray) { return myArray.constructor.toString().indexOf("Array") > -1; }</script></body>
</html>
测试结果
判断是否为日期。true判断是否为数组。true