[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'>
......
ÉùÃ÷£º
function person(name, country)
{
this.name = name;
this.country = country;
}
ʵÀý»¯¶ÔÏó£º
var theauthor = new person('Daniel', 'U.S.A.');
ÒýÓãº
function sayHello(objPerson)
{
return "Hello " + objPerson. ......
/*¼ì²éÊÇ·ñÑ¡Öи´Ñ¡¿ò2010Äê3ÔÂ6ÈÕ10:36:48*/
function checkSelect(id, msg){
var checks = $(id).getElementsByTagName("input");
var flag=false;
if(checks.length!=null){
for(var i=0;i<checks.length;i++){
if(checks[i].type=="checkbox" && checks[i].checked= ......
һ˵µ½javascript¾ÍÓÐÈË»áÏëµ½java£¬µ«ÊÇËûÃÇÍê»áÊÇÁ½»ØÊ£¬javascriptÊÇÃæÏò¶ÔÏóµÄ¶¯Ì¬ÀàÐ͵ÄÇø·Ö´óСдµÄ¿Í»§¶Ë½Å±¾ÓïÑÔ£¬Çë×¢Òâ´óСÓÐÇø±ð£¬ÃæÏò¶ÔÏó¡£javascriptÓÃ;ºÜ¶àµÄ£¬Ä¿Ç°¼¸ºõËùÓÐÖ÷Á÷ä¯ÀÀÆ÷¶¼Ö§³Ö¡£µ«ÊÇËûÓÐÒ»¸ö»µ´¦¾ÍÊDZäÁ¿£¬Èçvar
i=4;Õâ¸öi¾ÍÊÇintÀàÐ͵ģ¬var
s="hello",Õâ¸ösÊÇ×Ö·û´®ÀàÐ͵ģ ......
Ò»£¬javascriptµÄÁ÷³Ì¿ØÖÆÓï¾ä
if(x==null) »ò if(typeof(x) =='undefined')¿ÉÒÔ¼òµ¥Ð´if(!x)
×¢Ò⣺ÔÚjavascriptÖУ¬false,null,undefined,0,""¾ùÊÇfalse
¼´ var x;
´Ëʱ if(x==null)
if(typeof(x)=='undefined')¾ùÎªÕæ
¶þ£¬switchÓï¾ä
switch(±í´ïʽ)   ......