如何在JavaScript对象中嵌入私有成员

上传人:桔**** 文档编号:432599591 上传时间:2022-10-31 格式:DOC 页数:14 大小:161KB
返回 下载 相关 举报
如何在JavaScript对象中嵌入私有成员_第1页
第1页 / 共14页
如何在JavaScript对象中嵌入私有成员_第2页
第2页 / 共14页
如何在JavaScript对象中嵌入私有成员_第3页
第3页 / 共14页
如何在JavaScript对象中嵌入私有成员_第4页
第4页 / 共14页
如何在JavaScript对象中嵌入私有成员_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《如何在JavaScript对象中嵌入私有成员》由会员分享,可在线阅读,更多相关《如何在JavaScript对象中嵌入私有成员(14页珍藏版)》请在金锄头文库上搜索。

1、 最近,我开发一个项目Angular Cloud Data Connector, 帮助Angular开发者使用云数据,特别是Azure移动服务, 使用WEB标准,像索引数据库(indexed DB)。我尝试建立一种方式,使得JavaScript开发者能将私有成员嵌入到一个对象中。我解决这个问题的技术用到了我命名的闭包空间(closure space)。在这篇入门文章中,我要分享的是如何在你的项目中用它,及它对主流浏览器的性能和内存的影响。在深入学习前,咱们先说下,你为什么需要用到私有成员(private members), 还有一种替代方式来模拟私有成员。1. 为何要用私有成员(Private

2、 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

3、 成员仍然可以直接访问和修改。这也就是为何我们需要更加稳定可靠的方式,声明私有成员,它智能通过对象的方法来访问。2. 使用闭包空间(Closure Space)解决方法是使用闭包空间。每当内部函数 (inner fanction) 访问来自外部函数作用域的变量时,浏览器为你分配一段内存空间。有时很取巧,不过就我们的题目来讲,这算是一个完美的解决方案。我们在上个代码版本中添加这个特性:varcreateProperty=function(obj,prop,currentValue)Object.defineProperty(obj,prop,get:function()returncurrent

4、Value;,set:function(value)currentValue=value;,enumerable:true,configurable:true);varentity=;varmyVar=helloworld;createProperty(entity,property,myVar);示例中,createProperty 函数有一个 currentValue 变量,存在 get 和 set 方法。此变量会保存到 get 和 set 函数的闭包空间中。现在,只有这两个函数能看到和更新currentValue 变量! 任务完成!唯一需要警惕 caveat,警告,注意)的是源值 (my

5、Var) 仍可访问。下面给出另一个更健壮的版本(保护 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,property);采用

6、该函数, 即便源值都销毁(destructed,注:意思是不能直接赋值)了。到此大功告成了!3.性能考虑Performance Considerations现在咱们看看性能。很明显,比起一个简单的变量,闭包空间,甚或(对象)属性要慢的多,且更消耗资源。这就是本文更多关注普通方式和闭包空间机制差异的原因。为证明闭包空间机制并不比标准方式更消耗资源,我写了下面代码做个基准测试:htmlfont-family:HelveticaNeue,Helvetica;puting.varresults=document.getElementById(results);varsampleSize=1000000

7、;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=entitiespos

8、ition.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;,enumer

9、able: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+)varpo

10、sition=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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号