switch 模式java
parseInt(year, 10)
)1.对象直接量,JSON,正则表达式直接量
1.强制new,避免使用其余的内置构造函数:String()、Number()、Boolean()以及不一样种类的
Error()构造器,that=thisgit
命名空间方式github
优势:ajax
缺点正则表达式
模块模式(命名空间模式+IIFE+私有和特权成员模式+依赖声明模式)算法
MYAPP.utilities.module = (function (app, global) { return (function(){})() }(MYAPP, this))
沙箱模式segmentfault
Sandbox.modules = {}; Sandbox.modules.dom = function (box) {}; Sandbox('dom', 'event', function (box) { Sandbox('ajax', function(box) { }); });
链式调用模式设计模式
缺点:调用这样写的代码会更困难浏览器
myobj.method1("hello").method2().method3("world").method4();
代码复用模式缓存
类式继承1:默认模式
缺点:既继承了(父对象)“本身的属性”,也继承了原型中的属性。大部分状况下你可能并不须要“本身的属性”,由于它们更多是为实例对象添加的,并不用于复用。
function inherit(C, P) { C.prototype = new P(); }
类式继承2:借用构造函数
缺点:没法继承原型
function Child(a, c, b, d) { Parent.apply(this, arguments); }
类式继承3:借用并设置原型(1,2的缺点修复,接近java)
缺点:父构造函数被调用了两次,因此不是很高效
function Child(a, c, b, d) { Parent.apply(this, arguments); } Child.prototype = new Parent();
类式继承4:共享原型
缺点:修改原型影响全部的继承
function inherit(C, P) { C.prototype = P.prototype; }
类式继承5:临时构造函数
function inherit(C, P) { var F = function () {}; F.prototype = P.prototype; C.prototype = new F(); }
原型继承(现代继承模式)
var child = Object.create(parent);
借用方法
//call() example notmyobj.doStuff.call(myobj, param1, p2, p3); // apply() example notmyobj.doStuff.apply(myobj, [param1, p2, p3]);
var obj = { myprop: 'my value' };
var corolla = CarMaker.factory('Compact'); var solstice = CarMaker.factory('Convertible'); var cherokee = CarMaker.factory('SUV'); corolla.drive(); // "Vroom, I have 4 doors" solstice.drive(); // "Vroom, I have 2 doors" cherokee.drive(); // "Vroom, I have 17 doors"
var element; while (element = agg.next()) { // do something with the element ... console.log(element); }
var sale = new Sale(100); // the price is 100 dollars sale = sale.decorate('fedtax'); // add federal tax sale = sale.decorate('cdn'); // format using CDN sale.getPrice(); // "CDN$ 105.00"
var data = { first_name: "Super", last_name: "Man", age: "unknown", username: "o_O" }; validator.config = { first_name: 'isNonEmpty', age: 'isNumber', username: 'isAlphaNum' }; validator.validate(data); if (validator.hasErrors()) { console.log(validator.messages.join("\n")); }
var myevent = { // ... stop: function (e) { // others if (typeof e.preventDefault === "function") e.preventDefault(); if (typeof e.stopPropagation === "function") e.stopPropagation(); // IE if (typeof e.returnValue === "boolean") e.returnValue = false; if (typeof e.cancelBubble === "boolean") e.cancelBubble = true; } // ... };
scroll_per_second=throttle(scroll,1000)
counter=function(){ var i=0; return function(){ return ++i; } } counter()
paper.subscribe(joe.drinkCoffee); paper.subscribe(joe.sundayPreNap, 'monthly'); paper.daily(); paper.monthly();
减小DOM访问操做次数
事件处理:隔离应用逻辑,不要分发event对象
var MyApplication={ handleClick(event){ this.showPopup(event.clientX,event.clientY) }, showPopup:function(x,y){ } } b.addEventListener('click', function(event){ MyApplication.handleClick(event) }, false);
事件委托
Y.delegate('click', myHandler, "#click-wrap", "button");
加载策略
重构目的:改进软件设计,使软件更容易理解,帮助找到bug,提升产出。
重构法则:事不过三,三则重构。添加功能/修补错误/复审代码时重构,重构不如重写简单、项目的最后期限,应该避免重构。
糟糕代码:
重构建议:
从新组织函数
在对象之间搬移特性
从新组织数据
简化条件表达式
简化函数调用
处理归纳关系
大型重构
《Javascript设计模式》
《编写可维护的Javascript》
《重构——改善既有代码的设计》
《代码大全2》
Design-Patterns-in-Javascript