Javascript Best Practices
1. ×ÜÊÇʹÓà ‘var’
ÔÚJavaScript
ÖУ¬±äÁ¿²»ÊÇÈ«¾Ö·¶Î§µÄ¾ÍÊǺ¯Êý·¶Î§µÄ£¬Ê¹ÓÔvar”¹Ø¼ü´Ê½«ÊDZ£³Ö±äÁ¿¼ò½àÃ÷Á˵Ĺؼü¡£µ±ÉùÃ÷Ò»¸ö»òÕßÊÇÈ«¾Ö»òÕßÊǺ¯Êý¼¶£¨function-level£©µÄ±äÁ¿£¬Ðè×ÜÊÇÇ°ÖÔvar”¹Ø¼ü´Ê£¬ÏÂÃæµÄÀý×Ó½«Ç¿µ÷²»ÕâÑù×öDZÔÚµÄÎÊÌâ¡£
²»Ê¹Óà Var Ôì³ÉµÄÎÊÌâ
var i=0; // This is good - creates a global variable
function test() {
for (i=0; i<10; i++) {
alert("Hello World!");
}
}
test();
alert(i); // The global variable i is now 10!
ÒòΪ±äÁ¿º¯ÊýÖбäÁ¿ i ²¢Ã»ÓÐʹÓà var ʹÆä³ÉΪº¯Êý¼¶µÄ±äÁ¿£¬ÔÚÕâ¸öÀý×ÓÖÐËüÒýÓÃÁËÈ«¾Ö±äÁ¿¡£×ÜÊÇʹÓà var À´ÉùÃ÷È«¾Ö±äÁ¿ÊÇÒ»¸öºÜ¶àµÄ×ö·¨£¬µ«ÖÁ¹ØÖØÒªµÄÒ»µãÊÇʹÓà var ¶¨ÒåÒ»¸öº¯Êý·¶Î§µÄ±äÁ¿¡£ÏÂÃæÕâÁ½¸ö·½·¨ÔÚ¹¦ÄÜÉÏÊÇÏàͬµÄ£º
ÕýÈ·µÄº¯Êý
function test() {
var i=0;
for (i=0; i<10; i++) {
alert("Hello World!");
}
}
ÕýÈ·µÄº¯Êý
function test() {
for (var
i=0; i<10; i++) {
alert("Hello World!");
}
}
2. ÌØÐÔ¼ì²â¶ø·Çä¯ÀÀÆ÷¼ì²â
һЩ´úÂëÊÇдÀ´·¢ÏÖä¯ÀÀÆ÷°æ±¾²¢»ùÓÚÓû§ÕýʹÓõĿͻ§¶ËµÄ¶ÔÆäÖ´Ðв»Í¬ÐÐΪ¡£Õâ¸ö£¬×ܵÄÀ´Ëµ£¬ÊÇÒ»¸ö·Ç³£ÔãµÄʵ¼ù¡£¸üºÃµÄ·½·¨ÊÇʹÓÃÌØÐÔ¼ì²â£¬ÔÚʹ
ÓÃÒ»¸öÀÏä¯ÀÀÆ÷¿ÉÄܲ»Ö§³ÖµÄ¸ß¼¶µÄÌØÐÔ֮ǰ£¬Ê×Ïȼì²â£¨ä¯ÀÀÆ÷µÄ£©ÊÇ·ñÓÐÕâ¸ö¹¦ÄÜ»òÌØÐÔ£¬È»ºóʹÓÃËü¡£Õâµ¥¶À¼ì²âä¯ÀÀÆ÷°æ±¾À´µÃ¸üºÃ£¬¼´Ê¹ÄãÖªµÀËüµÄÐÔ
ÄÜ¡£Äã¿ÉÒÔÔÚ http://www.jibbering.com/faq/faq_notes/not_browser_detect.html
ÕÒµ½Ò»¸öÉîÈëÌÖÂÛÕâ¸öÎÊÌâµÄÎÄÕ¡£
Àý×Ó£º
if (document.getElementById) {
var element = document.getElementById('MyId');
}
else {
alert('Your browser lacks the capabilities required to run this script!');
}
3. ʹÓ÷½À¨ºÅ¼Ç·¨
µ±·ÃÎÊÓÉÖ´ÐÐʱ¾ö¶¨»òÕß°üÀ¨Òª²»ÄÜÓÔ.”ºÅ·ÃÎʵĶÔÏóÊôÐÔ£¬Ê¹Ó÷½À¨ºÅ¼Ç·¨¡£Èç¹ûÄã²»ÊÇÒ»¸ö¾Ñé·á¸»µÄJavascript
³ÌÐòÔ±£¬×ÜÊÇʹÓ÷½À¨ºÅÊÇÒ»¸ö²»´íµÄ×ö·¨
¶ÔÏóµÄÊôÐÔÓÉÁ½Ö̶ֹ¨µÄ·½·¨À´·ÃÎÊ£º”.”¼Ç·¨ºÍ”[ ]“·½À¨ºÅ¼Ç·¨£º
“.”ºÅ¼Ç·¨
MyObject.property
“[ ]“·½À¨ºÅ¼Ç·¨
MyObject["property"]
ʹÓÔ.”ºÅ£¬ÊôÐÔÃûÊÇÓ²´úÂ룬²»ÄÜÔÚÖ´ÐÐʱ¸Ä±ä¡£Ê¹ÓÔ[
Ïà¹ØÎĵµ£º
scrollHeight: »ñÈ¡¶ÔÏóµÄ¹ö¶¯¸ß¶È¡£
scrollLeft:ÉèÖûò»ñȡλÓÚ¶ÔÏó×ó±ß½çºÍ´°¿ÚÖÐÄ¿Ç°¿É¼ûÄÚÈݵÄ×î×ó¶ËÖ®¼äµÄ¾àÀë
scrollTop:ÉèÖûò»ñȡλÓÚ¶ÔÏó×¶ËºÍ´°¿ÚÖпɼûÄÚÈݵÄ×¶ËÖ®¼äµÄ¾àÀë
scrollWidth:»ñÈ¡¶ÔÏóµÄ¹ö¶¯¿í¶È
offsetHeight:»ñÈ¡¶ÔÏóÏà¶ÔÓÚ°æÃæ»òÓɸ¸×ø±ê offsetParent ÊôÐÔÖ¸¶¨µÄ¸¸×ø±êµÄ¸ß¶È
offsetL ......
ÕâÀïÃæµÄreturnº¬ÓÐһЩϸ½Ú֪ʶ£º
ÀýÈ磺onClick='return add_onclick()'Óë onClick='add_onclick()'µÄÇø±ð
JAVASCRIPTÔÚʼþÖе÷Óú¯ÊýʱÓÃreturn·µ»Øֵʵ¼ÊÉÏÊǶÔwindow.event.returnvalue½øÐÐÉèÖá£
¶ø¸ÃÖµ¾ö¶¨Á˵±Ç°²Ù×÷ÊÇ·ñ¼ÌÐø¡£
µ±·µ»ØµÄÊÇtrueʱ£¬½«¼ÌÐø²Ù×÷¡£
µ±·µ»ØÊÇfalseʱ£¬½«ÖжϲÙ×÷¡£
¶øÖ±½ÓÖ´ÐÐʱ£ ......
½üÈÕÊÕ¼¯ÉÏÍòÐдúÂ룬ÕûÀíһЩ³öÀ´¡£
//V8ÒýÇæµÄʵÏÖ
function ToInteger( n ) {
n = Number( n );
var sign = ( n < 0 ) ? -1 : 1;
if ( n != n ) {
return 0;
}
if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
......
Javascript
ÊÇÒ»¸öºÜÇ¿´óºÜÁé»îµÄÓïÑÔ£¬Äã¼È¿ÉÒÔ°ÑËüµ±×÷ÃæÏò¹ý³ÌµÄÓïÑÔ£¬Ò²¿ÉÒÔ°ÑËüµ±×÷ÃæÏò¶ÔÏóµÄÓïÑÔÀ´Óá£ÕâƪÖ÷Ҫ̽ÌÖÒ»ÏÂÈçºÎÓÃjavascriptÀ´ÊµÏÖÒ»¸ö
SingletonÀà¡£ÏàÐÅÓÐЩ±à³Ì¾ÑéµÄÅóÓÑÃǶÔSingletonģʽ¶¼ºÜÊìϤ¡£Singleton
ģʽÖ÷ÒªÓÃÀ´ÏÞÖÆÒ»¸öÀàµÄ¶à´ÎʵÀý»¯¡£Ò»°ãÀ´ËµÏµÍ³´´½¨µÄʵÀýÔ½ÉÙ£¬Õ¼ÓÃ×ÊÔ´Ô ......
¾³£»áÔÚjsÀïÓõ½Êý×é,±ÈÈç ¶à¸öÃû×ÖÏàͬµÄinput, ÈôÊǶ¯Ì¬Éú³ÉµÄ, Ìύʱ¾ÍÐèÒªÅжÏÆäÊÇ·ñÊÇÊý×é.
if(document.mylist.length != "undefined" ) {} Õâ¸öÓ÷¨ÓÐÎó.
ÕýÈ·µÄÊÇ if( typeof(document.mylist.length) != "undefined" ) {}
»ò if( !isNaN(document.mylist.length) ) {}
typeofµÄÔËËãÊý䶨Òå,·µ»ØµÄ¾ÍÊÇ "u ......