今天在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 = '<ul>';for (i = 0; i < length; i++) {items += '<li>' + messages[i].message + '</li>';}return items + '</ul>';}//goodfunction inbox(messages){items = [];for( i = 0; i < length ; i++){items[i] = messages[i].message;}return '<ul><li>' + items.join('</li><li>') + '</li></ul>';}
函數(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');
新聞熱點
疑難解答