JavaScript constructor ÊôÐÔ

¶¨ÒåºÍÓ÷¨

constructor ÊôÐÔ·µ»Ø¶Ô´´½¨´Ë¶ÔÏóµÄÊý×麯ÊýµÄÒýÓá£

Óï·¨

object.constructor

ʵÀý

Àý×Ó 1

ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓà constructor ÊôÐÔ£º

<script type="text/javascript">

var test=new Array();

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 an Array

TIY

Àý×Ó 2

ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓà constructor ÊôÐÔ£º

<script type="text/javascript">

function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.constructor);

</script>

Êä³ö£º

function employee(name, job, born)
{this.name = name; this.job = job; this.born = born;}

TIY

TIY

constructor
ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Õ¹Ê¾ÈçºÎʹÓà constructor ÊôÐÔ¡£
VUE