|
|
|
|
|
|
09 2012 档案
The Flyweight Pattern in Javascript
摘要:Flyweight Pattern,中文可译作享元模式。它的核心是分离对象的:内在属性和外部属性,然后共享内在属性,组装外在属性。看一个汽车的例子:/*Carclass,un-optimized.*/varCar=function(make,model,year,owner,tag,renewDate){this.make=make;this.model=model;this.year=year;this.owner=owner;this.tag=tag;this.renewDate=renewDate;};Car.prototype={getMake:function(){returnt..
阅读全文
The Facade Pattern in Javascript
摘要:这里拿给DOM元素设置样式的case来演绎在js中怎么使用Facade Pattern。 先看菜鸟的做法:varelement1=document.getElementById('foo');element1.style.color='red';varelement2=document.getElementById('bar');element2.style.color='red';varelement3=document.getElementById('baz');element3.style.color=
阅读全文
The Decorator Pattern in Javascript
摘要:还是拿自行车车店的例子,当自行车车店针对不同品牌不同车型的车可以自由定制一些东西时怎么办? 比如,顾客想给某种型号的车加一个照面灯。再比如,它们想给车刷个拉风的漆。 如果使用继承,那子类就成千上万了。 这个时候,就可以用到Decorator模式了:/*TheBicycleinterface.*/varBicycle=newInterface("Bicycle",["assemble","wash","ride","repair","getPrice"]);/*AcmeComf
阅读全文
Composite in Javascript
摘要:本文从简的演示利用Composite Pattern来动态创建form,它支持保存和还原上次输入的数据。 第一步,定义接口:varComposite=newInterface("Composite",["add","remove","getChild"]);varFormItem=newInterface("FormItem",["save","restore","getElement"]);你懂的,这里是基于single respon
阅读全文
Bridge Pattern in Javascript
摘要:一个比较简洁的例子:functiongetBeerById(e){ varid=this.id;asyncRequest('GET','beer.uri?id='+id,function(resp){console.log('RequestedBeer:'+resp.responseText);});}addEvent(element,'click',getBeerById);这里可以看到,它其实就是为DOM元素的click绑定一个方法,在这个方法里做一个异步请求,然后当请求成功后,log出response结果。 问题在于getB
阅读全文
Javascript中Factory的应用
摘要:这里拿Pro Javascript Design Pattern中的例子作为case。假如一家自行车店销售各种自行车:/*TheBicycleinterface.*/varBicycle=newInterface("Bicycle",["assemble","wash","ride","repair"]);/*Speedsterclass.*/varSpeedster=function(){}Speedster.prototype={assemble:function(){console.log
阅读全文
Parallel Programming Easier than ever using .NET Framework 4
摘要:The .NET Framework 4 Provides support for parallel loops and regions easily if you compare to the older versions of .Net such as using System.Threading now with the introduction of the Parallel class it now provides library-based data parallel operations such as for loops, for each loops, and invoke
阅读全文
Javascript中chaining的实现
摘要:如果你了解Jquery,你一定为它的chaining的便利而折服。而它的原理,其实也很简单,不过是每个方法都返回this对象而已。如下:(function($){function_$(elements){this._elements=[];for(vari=0;i<elements.length;i++){if(typeof(elements[i])=="string"){this._elements.push(document.getElementById(elements[i]));}else{this._elements.push(elements[i]);}}
阅读全文
Javascript中singleton的实现
摘要:最好的实现:varlangzi=window.langzi||{};langzi.singleton=window.langzi.singleton||(function(){varinstance=null;varconstructor=function(){return{id:1,name:"test",getMessage:function(){console.log("id:"+this.id+",name:"+this.name);}};};return{getInstance:function(){if(instance=
阅读全文
Javascript中怎么实现继承?
摘要:传统的继承:varPerson=function(name){ this.name=name||"defaultname";if(typeofPerson._initialized=="undefined"){Person.prototype.getName=function(){returnthis.name;};Person._initialized=true;}};varAuthor=function(name,books){Person.call(this,name);this.books=books||[];/*if(typeofAuthor.
阅读全文
Javascript中怎么定义类(私有成员、静态成员)?
摘要:本文将从一个简单例子演绎怎么定义一个具有私有成员、静态成员的类。 好,看简单原型:varBook=function(isbn,title,author){if(isbn===undefined){thrownewError('Bookconstructorrequiresanisbn.');}this.isbn=isbn;this.title=title||'Notitlespecified';this.author=author||'Noauthorspecified';};Book.prototype.display=function(){a
阅读全文
Javascript中如何实现interface机制
摘要:我们知道,ECMAScript中是没有interface一说的。虽然如此,参考《Pro Javascript Design Pattern》一书,哥算是找到方案了。 最简单的方案,就是通过文档说明了。简称方案1,代码如下:/*interfacecomposite{functionadd(child);functionremove(child);functiongetChild(index);}interfaceformItem{functionsave();}*/varcompositeForm=function(id,name){this.id=id;this.name=name;if(t.
阅读全文
你未必真正了解的Singleton
摘要:缘于http://www.cnblogs.com/TomXu/archive/2011/12/19/2291448.html这篇文章,发现胖哥对于singleton的分析仍然不是十分深入,借鉴CLR via C#,再次深入完美一下singleton。 经典的double-check locking: publicclassSingleton{privatestaticSingletoninstance;privatestaticreadonlyObjectsyncRoot=newObject();privateSingleton(){}publicstaticSingletonG...
阅读全文
浏览器内核的解析和对比
摘要:要搞清楚浏览器内核是什么,首先应该先搞清楚浏览器的构成。简单来说浏览器可以分为两部分,shell+内核。其中shell的种类相对比较多,内核则比较少。Shell是指浏览器的外壳:例如菜单,工具栏等。主要是提供给用户界面操作,参数设置等等。它是调用内核来实现各种功能的。内核才是浏览器的核心。内核是基于标记语言显示内容的程序或模块。也有一些浏览器并不区分外壳和内核。从Mozilla将Gecko独立出来后,才有了外壳和内核的明确划分。目前主流的浏览器有IE6、IE8、Mozilla、FireFox、Opera、Safari、Chrome、Netscape等。什么是浏览器内核浏览器内核又可以分成两部分
阅读全文
|
|