国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

如何正確使用javascript 來進行我們的程序開發

2019-11-20 14:24:59
字體:
來源:轉載
供稿:網友

今天在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');
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滨海县| 辽阳市| 新津县| 柞水县| 湖北省| 辽阳市| 炉霍县| 招远市| 西乌珠穆沁旗| 秦皇岛市| 普兰县| 确山县| 色达县| 自贡市| 巴东县| 梅州市| 惠水县| 永泰县| 隆子县| 杭州市| 灌云县| 宜宾县| 新余市| 疏勒县| 庆云县| 牟定县| 周口市| 裕民县| 娄底市| 贺兰县| 乌兰察布市| 德令哈市| 平安县| 泉州市| 东安县| 沙洋县| 天峻县| 达日县| 广宗县| 望都县| 江西省|