如何正确使用javascript 来进行我们的程序开发
今天在github上面找到了一个关于如何正确使用javascript来进行我们的程序开发.我就恬不知耻的来了个原创啊..坑爹啊.拿来和大家分享一下吧.
AmostlyreasonableapproachtoJavascript.
Types//类型
Objects//对象
Arrays//数组
Strings//字符串
Functions//函数
Properties//属性
Variables//变量
Hoisting//变量提升
ConditionalExpressions&Equality//条件表达式和等式.
Blocks//块代码
Comments//注释
Whitespace//空格
Commas//逗号
Semicolons//分号
TypeCasting&Coercion//类型转换
NamingConventions//命名规则
Accessors//访问
Constructors//构造器
Events//时间
Modules//模型
jQuery//
ECMAScript5Compatibility//ECMA5兼容
Testing//测试
Performance//性能
Resources//资源
IntheWild
Translation
TheJavaScriptStyleGuideGuide
Contributors
License
Types(类型)
原始类型:当访问一个原始类型的时候,其实直接访问该原始类型的内容.
string
number
boolean
null
undefined
varfoo=1,
bar=foo;
bar=9;
console.log(foo,bar);//=>1,9
复杂类型:当你访问一个复杂类型数据类型的时候,其实是通过引用访问该变量的值.
object
array
function
varfoo=[1,2]; bar=foo; bar[0]=9; console.log(foo[0],bar[0]);//=>9,9
object(对象)
使用对象字面量来创建对象(literal)
//bad varitem=newObject(); //good varitem={};
不要使用保留关键字作为对象的属性名.这在IE8下无法工作.
//bad varsuperman={ default:{clark:'kent'}, private:true }; //good varsuperman={ defaults:{clark:'kent'}, hidden:true };
array(数组)
同样使用字面量方法来创建数组
//bad varitems=newArray(); //good varitems=[];
如果你不知道数组的长度,那么使用Array的内置方法push进行插入操作
varsomeStack=[]; //bad someStack[someStack.length]='vein'; //good someStack.push('vein');
当你想要拷贝一个数组的时候,使用array.slice
varlen=items.length,//指的就是上面的内容... itemCopy=[], i; //bad for(i=0;i<len;++i){ itemCopy[i]=items[i]; } //good itemCopy=items.slice();//这里要注意了.这个我还真不知道...
Strings字符串
使用单引号(singlequotes)来包围字符串...//这里我没有找到合适的关于性能方面的解释,我个人也喜欢这么用,(穿的少总比穿得多好看点吧..你懂得..)
//bad varname="BobParr"; //good varname='BobParr'; //bad varfullName="Bob"+this.lastName; //good varfullName='Bob'+this.lastName;
字符串长于80个字符的时候需要使用字符串连接在多行进行编写..注意,如果过度使用,连接字符串将会影响性能(performance)
//bad varerrorMessage='ThisisasuperlongerrorthatwasthrownbecauseofBatman.WhenyoustoptothinkabouthowBatmanhadanythingtodowiththis,youwouldgetnowherefast.'; //bad varerrorMessage='Thisisasuperlongerrorthatwasthrownbecause\ ofBatman.WhenyoustoptothinkabouthowBatmanhadanythingtodo\ withthis,youwouldgetnowhere\ fast.'; //good varerrorMessage='Thisisasuperlongerrorthatwasthrownbecause'+ 'ofBatman.WhenyoustoptothinkabouthowBatmanhadanythingtodo'+ 'withthis,youwouldgetnowherefast.';
如果是有计划的建立一个数组,像下面这样.使用Array.join效果会更好..
varitems, messages, length, i; messages=[{ stat:'success', message:'Thisoneworked' },{ stat:'success', message:'Thisoneworked' },{ stat:'success', message:'Thisoneworked' } ]; length=messages.length; //bad functioninbox(messages){ items='<ul>'; for(i=0;i<length;i++){ items+='<li>'+messages[i].message+'</li>'; } returnitems+'</ul>'; } //good functioninbox(messages){ items=[]; for(i=0;i<length;i++){ items[i]=messages[i].message; } return'<ul><li>'+items.join('</li><li>')+'</li></ul>'; }
函数(Functions)
//匿名函数表达式.. varanonymous=function(){ returntrue; }; //命名函数表达式. varnamed=functionnamed(){ returntrue; }; //即时引用函数 (function(){ console.log('WelcometotheInternet.Pleasefollowme.'); })();
永远不要在非函数的块代码(if,while)中定义函数.相应的,在代码块中间函数赋值给外部的变量名..
//bad if(currentUser){ functiontest(){ console.log('Nope.'); } } //good vartest; if(currentUser){ test=function(){ console.log('Yup'); };//becarefulwiththesemi-colon. }
Properties(属性)
使用点语法来访问属性.
varluke={ jedi:true, age:28 }; //bad varisJedi=luke['jedi']; //good varisJedi=luck.jedi;
当使用变量访问对象属性时,使用[]方括号来访问
varluke={ jedi:true, age:28 }; functiongetProp(prop){ returnluke[prop]; } varisJedi=getProp('jedi');