今天在github 上面找到了一个关于如何正确使用javascript 来进行我们的程序开发.我就恬不知耻的来了个原创啊..坑爹啊.拿来和大家分享一下吧.
a mostly reasonable approach to javascript.
types //类型
objects //对象
arrays //数组
strings //字符串
functions //函数
properties //属性
variables //变量
hoisting //变量提升
conditional expressions & equality //条件表达式和等式.
blocks //块代码
comments //注释
whitespace //空格
commas //逗号
semicolons //分号
type casting & coercion //类型转换
naming conventions //命名规则
accessors //访问
constructors //构造器
events //时间
modules //模型
jquery //
ecmascript 5 compatibility //ecma 5 兼容
testing //测试
performance //性能
resources //资源
in the wild
translation
the javascript style guide guide
contributors
license
types (类型)
原始类型: 当访问一个原始类型的时候,其实直接访问该原始类型的内容.
string
number
boolean
null
undefined
var foo = 1,
bar = foo;
bar = 9;
console.log(foo,bar); //=> 1,9
复杂类型: 当你访问一个复杂类型数据类型的时候,其实是通过引用访问该变量的值.
object
array
function
var foo = [1,2];bar = foo;bar[0] = 9;console.log(foo[0],bar[0]); // => 9,9
object(对象)
使用对象字面量来创建对象 (literal)
//badvar item = new object();//goodvar item = {};
不要使用保留关键字作为对象的属性名.这在ie8下无法工作.
//badvar superman = {default: {clark: 'kent'},private: true};//goodvar superman = {defaults: {clark: 'kent'},hidden: true};
array(数组)
同样使用 字面量方法来创建数组
//badvar items = new array();//goodvar items = [];
如果你不知道数组的长度,那么使用array的内置方法push进行插入操作
var somestack = [];//badsomestack[somestack.length] = 'vein';//goodsomestack.push('vein');
当你想要拷贝一个数组的时候,使用array.slice
var len = items.length, //指的就是上面的内容...itemcopy = [],i;//badfor(i = 0; i < len ; ++i){itemcopy[i] = items[i];}//gooditemcopy = items.slice(); //这里要注意了.这个我还真不知道...
strings 字符串
使用单引号 (single quotes ) 来包围字符串...//这里我没有找到合适的关于性能方面的解释,我个人也喜欢这么用,(穿的少总比穿得多好看点吧..你懂得..)
//badvar name = bob parr;//goodvar name = 'bob parr';//badvar fullname = bob + this.lastname;//goodvar fullname = 'bob ' + this.lastname;
字符串长于80个字符的时候需要使用字符串连接在多行进行编写..注意,如果过度使用,连接字符串将会影响性能(performance)
// badvar errormessage = 'this is a super long error that was thrown because of batman. when you stop to think about how batman had anything to do with this, you would get nowhere fast.';// badvar errormessage = 'this is a super long error that was thrown because \of batman. when you stop to think about how batman had anything to do \with this, you would get nowhere \fast.';// goodvar errormessage = 'this is a super long error that was thrown because ' +'of batman. when you stop to think about how batman had anything to do ' +'with this, you would get nowhere fast.';
如果是有计划的 建立一个数组,像下面这样.使用array.join 效果会更好..
var items,messages,length,i;messages = [{stat: 'success',message: ' this one worked'},{stat: 'success',message: ' this one worked'},{stat: 'success',message: ' this one worked'}];length = messages.length;//badfunction inbox(messages){items = '';for (i = 0; i < length; i++) {items += '' + messages[i].message + '';}return items + '';}//goodfunction inbox(messages){items = [];for( i = 0; i < length ; i++){items[i] = messages[i].message;}return '' + items.join('') + '';}
函数(functions)
//匿名函数表达式..var anonymous = function(){return true;};// 命名函数表达式.var named = function named(){return true;};//即时引用函数(function(){console.log('welcome to the internet. please follow me.');})();
永远不要在非函数的块代码(if,while)中定义函数.相应的,在代码块中间函数赋值给外部的变量名..
//badif(currentuser){function test(){console.log('nope.');}}//goodvar test;if(currentuser){test = function(){console.log('yup'); }; //be careful with the semi-colon.}
properties (属性)
使用点语法来访问属性.
var luke = {jedi: true,age: 28};//badvar isjedi = luke['jedi'];//goodvar isjedi = luck.jedi;
当使用变量访问对象属性时,使用 [] 方括号来访问
var luke = {jedi: true,age: 28};function getprop(prop) {return luke[prop];}var isjedi = getprop('jedi');