發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導(dǎo)圖
隨筆
相冊
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
// bad if (test) return false; // good if (test) return false; // good if (test) { return false; } // bad function() { return false; } // good function() { return false; }
// bad // make() returns a new element // based on the passed in tag name // // @param <String> tag // @return <Element> element function make(tag) { // ...stuff... return element; } // good /** * make() returns a new element * based on the passed in tag name * * @param <String> tag * @return <Element> element */ function make(tag) { // ...stuff... return element; }
// bad var active = true; // is current tab // good // is current tab var active = true; // bad function getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type; } // good function getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type; }
對于一些問題,注釋前加FIXME或TODO,這樣將快速幫助開發(fā)者快速明白代碼意圖。
使用 // FIXME: 注釋問題
function Calculator() { // FIXME: shouldn't use a global here total = 0; return this; }
使用 // TODO: 注釋問題的解決方案
function Calculator() { // TODO: total should be configurable by an options param this.total = 0; return this; }
// => this.reviewScore = 9; // bad var totalScore = this.reviewScore + ''; // good var totalScore = '' + this.reviewScore; // bad var totalScore = '' + this.reviewScore + ' total score'; // good var totalScore = this.reviewScore + ' total score';
對于數(shù)字轉(zhuǎn)換,使用parseInt,而且要帶著類型轉(zhuǎn)換的基數(shù)。
如果parseInt成為你的瓶頸,處于性能原因,需要你使用“位移”操作。那么請寫下注釋解釋你這樣做的原因。
var inputValue = '4'; // bad var val = new Number(inputValue); // bad var val = +inputValue; // bad var val = inputValue >> 0; // bad var val = parseInt(inputValue); // good var val = Number(inputValue); // good var val = parseInt(inputValue, 10); // good /** * parseInt 使我的代碼變慢. * 為了提高速度,使用位移操作讓字符串強(qiáng)制轉(zhuǎn)化為數(shù)字。 */ var val = inputValue >> 0;
布爾
var age = 0; // bad var hasAge = new Boolean(age); // good var hasAge = Boolean(age); // good var hasAge = !!age;
function Jedi() { console.log('new jedi'); } // bad Jedi.prototype = { fight: function fight() { console.log('fighting'); }, block: function block() { console.log('blocking'); } }; // good Jedi.prototype.fight = function fight() { console.log('fighting'); }; Jedi.prototype.block = function block() { console.log('blocking'); };
// bad Jedi.prototype.jump = function() { this.jumping = true; return true; }; Jedi.prototype.setHeight = function(height) { this.height = height; }; var luke = new Jedi(); luke.jump(); // => true luke.setHeight(20) // => undefined // good Jedi.prototype.jump = function() { this.jumping = true; return this; }; Jedi.prototype.setHeight = function(height) { this.height = height; return this; }; var luke = new Jedi(); luke.jump() .setHeight(20);
我們可以自定義一個toString()方法。——要確保它能正常運(yùn)行,而且不會產(chǎn)生其他影響。
function Jedi(options) { options || (options = {}); this.name = options.name || 'no name'; } Jedi.prototype.getName = function getName() { return this.name; }; Jedi.prototype.toString = function toString() { return 'Jedi - ' + this.getName(); };
來自: 昵稱10504424 > 《Js》
0條評論
發(fā)表
請遵守用戶 評論公約
類和模塊
如果兩個實(shí)例都從同一個原型上繼承屬性,則他們它們是同一個類的實(shí)例,類的所有實(shí)例對象都從同一個原型對象上繼承屬性。1 //變量只能被類的實(shí)例方法訪問,類的外面不可見 2 function Range(from,to){ 3...
aaa
aaaModel.prototype.addCartButtonClick = function(event){var row = event.bindingContext.$object;if( rows.length == 0 ){this.comp("cartData").newData({defaultValues:[{"fMenuI...
無廢話ExtJs 入門教程二十二[動態(tài)復(fù)選框:RemoteCheckboxGroup]
js刪除數(shù)組里的某個元素
js刪除數(shù)組里的某個元素。Array.prototype.indexOf = function(val) {for (var i = 0;i++) {if (this[i] == val) return i;Array.prototype.remove = function(val) {var index = this.indexOf(val);這...
三張圖搞懂JavaScript的原型對象與原型鏈
三張圖搞懂JavaScript的原型對象與原型鏈。//A {}(即構(gòu)造器function A 的原型對象)console.log(a.__proto__.__proto__); //Object {}...
ES6學(xué)習(xí)--let關(guān)鍵字
ES6學(xué)習(xí)ES6中新增加了let命令,用它聲明的變量,只在代碼塊內(nèi)有效。console.log(a);i++){console.log(i);}console.log(i);在for循環(huán)中{}包裹起來的部分是代碼塊;但是{}包裹起來的部分,能夠訪問到for...
js數(shù)組遍歷和對象遍歷
js數(shù)組遍歷和對象遍歷。針對js各種遍歷作一個總結(jié)分析,從類型用處:分?jǐn)?shù)組遍歷和對象遍歷;JS數(shù)組遍歷:arr.forEach(function(value,i){ console.log(''''''''for...
2016十家公司前端面試小記
回到前面說的題目,大致實(shí)現(xiàn)思路就是創(chuàng)建一個類或是匿名函數(shù),在bind和trigger函數(shù)外層作用域創(chuàng)建一個字典對象,用于存儲注冊的事件及響應(yīng)函數(shù)列表,bind時,如果字典沒有則創(chuàng)建一個,key是事件名稱,v...
創(chuàng)建xmlhttp請求&管理請求
微信掃碼,在手機(jī)上查看選中內(nèi)容