资源预览内容
第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
第9页 / 共14页
第10页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
最近,我开发一个项目Angular Cloud Data Connector, 帮助Angular开发者使用云数据,特别是Azure移动服务, 使用WEB标准,像索引数据库(indexed DB)。我尝试建立一种方式,使得JavaScript开发者能将私有成员嵌入到一个对象中。我解决这个问题的技术用到了我命名的闭包空间(closure space)。在这篇入门文章中,我要分享的是如何在你的项目中用它,及它对主流浏览器的性能和内存的影响。在深入学习前,咱们先说下,你为什么需要用到私有成员(private members), 还有一种替代方式来模拟私有成员。1. 为何要用私有成员(Private Members)当你用JavaScript 创建一个对象时,可以声明值成员(value members)。 如果你打算控制对它们的读/写访问操作,可以如下声明:varentity=;entity._property=helloworld;Object.defineProperty(entity,property,get:function()returnthis._property;,set:function(value)this._property=value;,enumerable:true,configurable:true);这样实现,你能完全控制读和写操作。问题在于_property 成员仍然可以直接访问和修改。这也就是为何我们需要更加稳定可靠的方式,声明私有成员,它智能通过对象的方法来访问。2. 使用闭包空间(Closure Space)解决方法是使用闭包空间。每当内部函数 (inner fanction) 访问来自外部函数作用域的变量时,浏览器为你分配一段内存空间。有时很取巧,不过就我们的题目来讲,这算是一个完美的解决方案。我们在上个代码版本中添加这个特性:varcreateProperty=function(obj,prop,currentValue)Object.defineProperty(obj,prop,get:function()returncurrentValue;,set:function(value)currentValue=value;,enumerable:true,configurable:true);varentity=;varmyVar=helloworld;createProperty(entity,property,myVar);示例中,createProperty 函数有一个 currentValue 变量,存在 get 和 set 方法。此变量会保存到 get 和 set 函数的闭包空间中。现在,只有这两个函数能看到和更新currentValue 变量! 任务完成!唯一需要警惕 caveat,警告,注意)的是源值 (myVar) 仍可访问。下面给出另一个更健壮的版本(保护 myVar 变量):varcreateProperty=function(obj,prop)varcurrentValue=objprop;Object.defineProperty(obj,prop,get:function()returncurrentValue;,set:function(value)currentValue=value;,enumerable:true,configurable:true);varentity=property:helloworld;createProperty(entity,.baiyuewang.netproperty);采用该函数, 即便源值都销毁(destructed,注:意思是不能直接赋值)了。到此大功告成了!3.性能考虑Performance Considerations现在咱们看看性能。很明显,比起一个简单的变量,闭包空间,甚或(对象)属性要慢的多,且更消耗资源。这就是本文更多关注普通方式和闭包空间机制差异的原因。为证明闭包空间机制并不比标准方式更消耗资源,我写了下面代码做个基准测试:htmlfont-family:HelveticaNeue,Helvetica;puting.varresults=document.getElementById(results);varsampleSize=1000000;varopCounts=1000000;varentities=;setTimeout(function()/Creatingentitiesfor(varindex=0;indexsampleSize;index+)entities.push(property:helloworld(+index+);/Randomreadsvarstart=newDate().getTime();for(index=0;indexopCounts;index+)varposition=Math.floor(Math.random()*entities.length);vartemp=entitiesposition.property;varend=newDate().getTime();results.innerHTML=Results:Usingmemberaccess:+(end-start)+ms;,0);setTimeout(function()/Closurespace=varcreateProperty=function(obj,prop,currentValue)Object.defineProperty(obj,prop,get:function()returncurrentValue;,set:function(value)currentValue=value;,enumerable:true,configurable:true);/Addingpropertyandusingclosurespacetosaveprivatevaluefor(varindex=0;indexsampleSize;index+)varentity=entitiesindex;varcurrentValue=entity.property;createProperty(entity,property,currentValue);/Randomreadsvarstart=newDate().getTime();for(index=0;indexopCounts;index+)varposition=Math.floor(Math.random()*entities.length);vartemp=entitiesposition.property;varend=newDate().getTime();results.innerHTML+=Usingclosurespace:+(end-start)+ms;,0);setTimeout(function()/Usinglocalmember=/Addingpropertyandusinglocalmembertosaveprivatevaluefor(varindex=0;indexsampleSize;index+)varentity=entitiesindex;entity._property=entity.property;Object.defineP
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号