DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 如何正確使用javascript 來進行我們的程序開發
如何正確使用javascript 來進行我們的程序開發
編輯:關於JavaScript     

今天在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)

//bad
var item = new Object();
//good
var item = {};

不要使用保留關鍵字作為對象的屬性名.這在IE8下無法工作.

//bad
var superman = {
default: {clark: 'kent'},
private: true
};
//good
var superman = {
defaults: {clark: 'kent'},
hidden: true
};

array(數組)
同樣使用 字面量方法來創建數組

//bad
var items = new Array();
//good
var items = [];

如果你不知道數組的長度,那麼使用Array的內置方法push進行插入操作

var someStack = [];
//bad
someStack[someStack.length] = 'vein';
//good
someStack.push('vein');

當你想要拷貝一個數組的時候,使用array.slice

var len = items.length, //指的就是上面的內容...
itemCopy = [],
i;
//bad
for(i = 0; i < len ; ++i){
itemCopy[i] = items[i];
}
//good
itemCopy = items.slice(); //這裡要注意了.這個我還真不知道...

Strings 字符串
使用單引號 (single quotes ) 來包圍字符串...//這裡我沒有找到合適的關於性能方面的解釋,我個人也喜歡這麼用,(穿的少總比穿得多好看點吧..你懂得..)

//bad
var name = "Bob Parr";
//good
var name = 'Bob Parr';
//bad
var fullName = "Bob " + this.lastName;
//good
var fullName = 'Bob ' + this.lastName;

字符串長於80個字符的時候需要使用字符串連接在多行進行編寫..注意,如果過度使用,連接字符串將會影響性能(performance)

// bad
var 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.';
// bad
var 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.';
// good
var 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;
//bad
function inbox(messages){
items = '<ul>';
for (i = 0; i < length; i++) {
items += '<li>' + messages[i].message + '</li>';
}
return items + '</ul>';
}
//good
function 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)中定義函數.相應的,在代碼塊中間函數賦值給外部的變量名..

//bad
if(currentUser){
function test(){
console.log('Nope.');
}
}
//good
var test;
if(currentUser){
test = function(){
console.log('Yup'); 
}; //be careful with the semi-colon.
}

Properties (屬性)
使用點語法來訪問屬性.

var luke = {
jedi: true,
age: 28
};
//bad
var isJedi = luke['jedi'];
//good
var isJedi = luck.jedi;

當使用變量訪問對象屬性時,使用 [] 方括號來訪問

var luke = {
jedi: true,
age: 28
};
function getProp(prop) {
return luke[prop];
}
var isJedi = getProp('jedi');
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved