[Javascript]typeofº¯ÊýÓëconstructorÊôÐÔ
½ñÌìżȻ¿´µ½ÁËconstructorÕâ¸ö¶«¶«£¬²»Ã÷°×ÓÃ;¡£Õҵ㶫Î÷±¸Íü
typeofº¯ÊýµÄÓ÷¨¿ÉÄܲ»ÓÃÎÒ¶à˵£¬´ó¼Ò¶¼ÖªµÀÔõôÓ᣶øconstructorÊôÐÔ´ó¼Ò¿ÉÄܾÍÄ°Éúµã¡£ÔÚ¡¶¾«Í¨JavaScript¡·Õâ±¾ÊéÖÐÓÐÌáµ½constructµÄÓ÷¨£¬µ«ÎÒÓÃ×Ô¼ºµÄ¼¸¸öä¯ÀÀÆ÷£¨IE7.0 / Firefox1.9 / Opera9.50£©²âÊԵĽá¹ûÈ´ºÍÊéÉÏ˵µÄ²»Ò»Ñù¡£µ«ÊÇÈÔÈ»ÊÇÓа취ͨ¹ýconstructorÊôÐÔÀ´¼ì²é±äÁ¿ÀàÐ͵ġ£
ÕâÀïÏȲ¹³äһϣ¬ÎªÊ²Ã´Ã÷Ã÷ÓÐtypeofº¯Êý¿ÉÒԺܷ½±ãµØÓÃÀ´¼ì²âÀàÐÍ£¬»¹ÒªÓÃconstructorÄØ£¿
ÒòΪtypeof»á°ÑËùÓеÄÊý×éÀàÐÍÒÔ¼°Óû§×Ô¶¨ÒåÀàÐÍÅжÏΪobject£¬´Ó¶øÎÞ·¨ÖªµÀ¸üÈ·ÇеÄÐÅÏ¢¡£¶øconstructorÈ´¿ÉÒÔ½â¾öÕâ¸öÎÊÌâ¡£
ok£¬Ã÷°×ÁËÎÒÃÇΪʲôҪÓÃconstructor£¬ÏÖÔÚÈÃÎÒ´ø´ó¼ÒÒ»²½²½ÈÏʶһÏÂtypeofºÍconstructorÓ÷¨Ö®¼äµÄ²îÒì°É¡«
Ê×ÏÈÎÒÃÇÔËÐÐÒ»ÏÂÏÂÃæÕâ¶Î´úÂ룺
1 var i;
2 alert(typeof(i));
3 alert(i.constructor);
Õâ3ÐдúÂë¸æËßÄãʲôÇé¿öÏ¿ÉÒÔÓÃconstructor¡£
Äã¿ÉÒÔ¿´µ½µÚ2Ðзµ»ØÁË×Ö·û´®'undefined'£¬¶øµÚÈýÐÐÔò·¢ÉúÁË´íÎó£¬ÔÒòÊÇi±äÁ¿»¹Ã»ÓÐÀàÐͶ¨Ò壬×ÔȻҲûÓÐconstructorµÄ´æÔÚ¡£
´ÓÕâÒ»µãÉÏ¿´£¬typeof¿ÉÒÔ¼ì²éµ½±äÁ¿ÊÇ·ñÓж¨Ò壬¶øconstructÖ»Äܼì²éÒѶ¨Òå±äÁ¿µÄÀàÐÍ¡£
ÔÙÔËÐÐÒ»ÏÂÏÂÃæÕâ¶Î´úÂ룺
1 var i = 2;
2 alert(typeof(i));
3 alert(i.constructor);
4 alert(typeof(i.constructor));
Äã»á¿´µ½µÚ2Ðзµ»ØÁË×Ö·û´®'number’£¬µÚ3Ðзµ»ØÁËÒ»´®ÀàËƺ¯Êý¶¨ÒåµÄ´úÂë×Ö·û´®£¨Õâ¾ÍÊǸú¡¶¾«Í¨JavaScript¡·Ò»ÊéÖнéÉܵIJ»Ò»ÑùµÄµØ·½£©¡£
ÎÒÃÇÔÙÓÃtypeof¼ì²éÒ»ÏÂconstructorµ½µ×ÊǸöʲôÑùÀàÐ͵ÄÊôÐÔ£¬µÚ4Ðзµ»Ø½á¹û'function'£¬Ò²¾ÍÊÇ˵£¬Êµ¼ÊÉÏconstructorÊÇÒ»¸öº¯Êý£¬¸üÈ·ÇеØ˵ÊÇÒ»¸ö¹¹Ô캯Êý¡£ÕâʱÄã¾Í¿ÉÒÔÖªµÀ£¬ÎªÊ²Ã´constructor¿ÉÒÔ¼ì²é³ö¸÷ÖÖÀàÐÍÁË¡£
ÓоÑéµÄ³ÌÐòÔ±¿´µ½ÕâÀïÓ¦¸ÃÖªµÀÒªÔõôÀûÓÃconstructorÀ´¼ì²é±äÁ¿ÀàÐÍÁË¡£·½·¨ÓжàÖÖ£¬ÕâÀïÌṩһÖֱȽÏÈÝÒ×Àí½âµÄ·½·¨¡£
ÆäʵÏë·¨ºÜ¼òµ¥£¬¾ÍÊÇ°Ñconstrucorת»¯Îª×Ö·û´®£¬Í¨¹ýÑ°ÕÒÆ¥Åä×Ö·û´®£¨functionÃû£©À´È·¶¨ÊÇ·ñÖ¸¶¨ÀàÐÍ¡£ÈçÏÂÀý×Ó£º
1 function user() {};
2 var i = new user();
3 alert((i.constructor+'').match(/user/) == null);
Õâ½ö½öÊǸö¼òµ¥µÄÀý×Ó¡£Èç¹û·µ»ØtrueÔò±äÁ¿i²»ÊÇuserÀàÐÍ£¬·µ»ØfalseÔò±äÁ¿ÊÇuserÀàÐÍ¡£
µ±È»£¬ÕâÑù¼ì²âÊDz»¹»¾«È·µÄ£¬±ÈÈçÆäʵËûÊÇÒ»¸ömyuserÀàÐ͵Äʱºò£¬Í¬Ñù»á±»ÈÏΪÊÇuserÀà¡£ËùÒÔÄãÐèÒªÊéд¸ü¾«È·µÄÕýÔò±í´ïʽȥ½øÐÐÆ¥Åä¡£
¿ÉÒÔÕâÑù¼
Ïà¹ØÎĵµ£º
ʵÀýÈçÏ£ºjavascript.htmlÎļþ
<html>
<head>
<title>JavaScriptÑéÖ¤</title>
</head>
<script language='JavaScript'>
......
ÔÚJavaScriptÖд´½¨Ð¶ÔÏó
£¨ÀîÏþ»ª¡¡2001Äê04ÔÂ29ÈÕ 01:50£©
¡¡¡¡Ê¹ÓÃJavaScript¿ÉÒÔ´´½¨×Ô¼ºµÄ¶ÔÏó¡£ËäÈ»JavaScriptÄÚ²¿ºÍä¯ÀÀÆ÷±¾ÉíµÄ¹¦ÄÜÒÑÊ®·ÖÇ¿´ó£¬µ«JavaScript»¹ÊÇÌṩÁË´´½¨Ò»¸öжÔÏóµÄ·½·¨¡£Ê¹Æä²»±ØÏñ³¬Îı¾±êʶÓïÑÔÄÇÑù£¬ÇóÓÚ»òÆäËü¶àýÌ幤¾ß£¬¾ÍÄÜÍê³ÉÐí¶à¸´ÔӵŤ×÷¡£
¡¡¡¡ÔÚJavaScriptÖд´½¨Ò»¸öÐÂµÄ¶Ô ......
ÉùÃ÷£º
function person(name, country)
{
this.name = name;
this.country = country;
}
ʵÀý»¯¶ÔÏó£º
var theauthor = new person('Daniel', 'U.S.A.');
ÒýÓãº
function sayHello(objPerson)
{
return "Hello " + objPerson. ......
һ˵µ½javascript¾ÍÓÐÈË»áÏëµ½java£¬µ«ÊÇËûÃÇÍê»áÊÇÁ½»ØÊ£¬javascriptÊÇÃæÏò¶ÔÏóµÄ¶¯Ì¬ÀàÐ͵ÄÇø·Ö´óСдµÄ¿Í»§¶Ë½Å±¾ÓïÑÔ£¬Çë×¢Òâ´óСÓÐÇø±ð£¬ÃæÏò¶ÔÏó¡£javascriptÓÃ;ºÜ¶àµÄ£¬Ä¿Ç°¼¸ºõËùÓÐÖ÷Á÷ä¯ÀÀÆ÷¶¼Ö§³Ö¡£µ«ÊÇËûÓÐÒ»¸ö»µ´¦¾ÍÊDZäÁ¿£¬Èçvar
i=4;Õâ¸öi¾ÍÊÇintÀàÐ͵ģ¬var
s="hello",Õâ¸ösÊÇ×Ö·û´®ÀàÐ͵ģ ......