constructor 属性返回变量或对象的构造函数。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>constructor 属性返回变量或对象的构造函数。</title>
</head>
<body>

<p ></p>
<script>
document.getElementById("demo").innerHTML = 
    "john".constructor + "<br>" +
    (3.14).constructor + "<br>" +
    false.constructor + "<br>" +
    [1,2,3,4].constructor + "<br>" +
    {name:'john', age:34}.constructor + "<br>" +
    new Date().constructor + "<br>" +
    function () {}.constructor;
</script>

</body>
</html>

运行结果:

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 ) > -1;
  }
</script>

</body>

</html>

 

测试结果

判断是否为日期。

true

判断是否为数组。

true

 

相关文章: