JavaScript constructor 属性
JavaScript基础 2022-06-08 11:02:55小码哥的IT人生shichen
JavaScript constructor 属性
定义和用法
constructor 属性返回对创建此对象的 Boolean 函数的引用。
语法
object.constructor
实例
在本例中,我们将展示如何使用 constructor 属性:
<script type="text/javascript">
var test=new Boolean();
if (test.constructor==Array
)
{
document.write("This is an Array");
}
if (test.constructor==Boolean
)
{
document.write("This is a Boolean");
}
if (test.constructor==Date
)
{
document.write("This is a Date");
}
if (test.constructor==String
)
{
document.write("This is a String");
}
</script>
输出:
This is a Boolean
完整实例【TIY】:
<html>
<body>
<script type="text/javascript">
var test=new Boolean();
if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
TIY
完整实例【constructor】:
<html>
<body>
<script type="text/javascript">
var test=new Boolean();
if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
在本例中,我们将展示如何使用 constructor 属性。