pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <bdo id='iKI1S'></bdo><ul id='iKI1S'></ul>

<small id='iKI1S'></small><noframes id='iKI1S'>

<tfoot id='iKI1S'></tfoot>
    1. <legend id='iKI1S'><style id='iKI1S'><dir id='iKI1S'><q id='iKI1S'></q></dir></style></legend>

      1. <i id='iKI1S'><tr id='iKI1S'><dt id='iKI1S'><q id='iKI1S'><span id='iKI1S'><b id='iKI1S'><form id='iKI1S'><ins id='iKI1S'></ins><ul id='iKI1S'></ul><sub id='iKI1S'></sub></form><legend id='iKI1S'></legend><bdo id='iKI1S'><pre id='iKI1S'><center id='iKI1S'></center></pre></bdo></b><th id='iKI1S'></th></span></q></dt></tr></i><div class="jxhgfl3" id='iKI1S'><tfoot id='iKI1S'></tfoot><dl id='iKI1S'><fieldset id='iKI1S'></fieldset></dl></div>

        如何獲取 javascript 對象引用或引用計數?

        How to get javascript object references or reference count?(如何獲取 javascript 對象引用或引用計數?)
            <tbody id='U3kzF'></tbody>
          <i id='U3kzF'><tr id='U3kzF'><dt id='U3kzF'><q id='U3kzF'><span id='U3kzF'><b id='U3kzF'><form id='U3kzF'><ins id='U3kzF'></ins><ul id='U3kzF'></ul><sub id='U3kzF'></sub></form><legend id='U3kzF'></legend><bdo id='U3kzF'><pre id='U3kzF'><center id='U3kzF'></center></pre></bdo></b><th id='U3kzF'></th></span></q></dt></tr></i><div class="8gvutya" id='U3kzF'><tfoot id='U3kzF'></tfoot><dl id='U3kzF'><fieldset id='U3kzF'></fieldset></dl></div>
        • <tfoot id='U3kzF'></tfoot>

          <small id='U3kzF'></small><noframes id='U3kzF'>

          • <legend id='U3kzF'><style id='U3kzF'><dir id='U3kzF'><q id='U3kzF'></q></dir></style></legend>
              <bdo id='U3kzF'></bdo><ul id='U3kzF'></ul>

                  本文介紹了如何獲取 javascript 對象引用或引用計數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  • 是否可以確定一個 javascript 對象是否有多個引用?
                  • 或者如果它有引用除了我正在訪問它的那個?
                  • 或者甚至只是為了獲取引用計數本身?
                  • 我能否從 javascript 本身中找到這些信息,或者我是否需要跟蹤我自己的引用計數器.

                  顯然,我的代碼必須至少有一個對它的引用才能訪問該對象.但我想知道是否有其他引用它,或者我的代碼是否是唯一可以訪問它的地方.如果沒有其他對象引用它,我希望能夠刪除該對象.

                  Obviously, there must be at least one reference to it for my code access the object. But what I want to know is if there are any other references to it, or if my code is the only place it is accessed. I'd like to be able to delete the object if nothing else is referencing it.

                  如果您知道答案,則無需閱讀此問題的其余部分.以下只是一個示例,以使事情更清楚.

                  If you know the answer, there is no need to read the rest of this question. Below is just an example to make things more clear.

                  在我的應用程序中,我有一個名為 contactsRepository 對象實例,其中包含 ALL 我的聯系人數組.還有多個 Collection 對象實例,例如 friends 集合和 coworkers 集合.每個集合都包含一個數組,其中包含來自 contacts Repository 的一組不同項.

                  In my application, I have a Repository object instance called contacts that contains an array of ALL my contacts. There are also multiple Collection object instances, such as friends collection and a coworkers collection. Each collection contains an array with a different set of items from the contacts Repository.

                  為了使這個概念更具體,請考慮下面的代碼.Repository 對象的每個實例都包含一個特定類型的所有項目的列表.您可能有一個聯系人存儲庫和一個單獨的事件存儲庫.為簡單起見,您可以獲取、添加和刪除項目,并通過構造函數添加許多項目.

                  To make this concept more concrete, consider the code below. Each instance of the Repository object contains a list of all items of a particular type. You might have a repository of Contacts and a separate repository of Events. To keep it simple, you can just get, add, and remove items, and add many via the constructor.

                  var Repository = function(items) {
                    this.items = items || [];
                  }
                  Repository.prototype.get = function(id) {
                    for (var i=0,len=this.items.length; i<len; i++) {
                      if (items[i].id === id) {
                        return this.items[i];
                      }
                    }
                  }
                  Repository.prototype.add = function(item) {
                    if (toString.call(item) === "[object Array]") {
                      this.items.concat(item);
                    }
                    else {
                      this.items.push(item);
                    }
                  }
                  Repository.prototype.remove = function(id) {
                    for (var i=0,len=this.items.length; i<len; i++) {
                      if (items[i].id === id) {
                        this.removeIndex(i);
                      }
                    }
                  }
                  Repository.prototype.removeIndex = function(index) {
                    if (items[index]) {
                      if (/* items[i] has more than 1 reference to it */) {
                        // Only remove item from repository if nothing else references it
                        this.items.splice(index,1);
                        return;
                      }
                    }
                  }  
                  

                  注意 remove 中帶有注釋的行.如果沒有其他對象引用該項目,我只想從我的對象主存儲庫中刪除該項目.這是集合:

                  Note the line in remove with the comment. I only want to remove the item from my master repository of objects if no other objects have a reference to the item. Here's Collection:

                  var Collection = function(repo,items) {
                    this.repo = repo;
                    this.items = items || [];
                  }
                  Collection.prototype.remove = function(id) {
                    for (var i=0,len=this.items.length; i<len; i++) {
                      if (items[i].id === id) {
                        // Remove object from this collection
                        this.items.splice(i,1);
                        // Tell repo to remove it (only if no other references to it)
                        repo.removeIndxe(i);
                        return;
                      }
                    }
                  }
                  

                  然后這段代碼使用RepositoryCollection:

                  var contactRepo = new Repository([
                      {id: 1, name: "Joe"},
                      {id: 2, name: "Jane"},
                      {id: 3, name: "Tom"},
                      {id: 4, name: "Jack"},
                      {id: 5, name: "Sue"}
                    ]);
                  
                  var friends = new Collection(
                    contactRepo,
                    [
                      contactRepo.get(2),
                      contactRepo.get(4)
                    ]
                  );
                  
                  var coworkers = new Collection(
                    contactRepo,
                    [
                      contactRepo.get(1),
                      contactRepo.get(2),
                      contactRepo.get(5)
                    ]
                  );
                  
                  contactRepo.items; // contains item ids 1, 2, 3, 4, 5 
                  friends.items;  // contains item ids 2, 4
                  coworkers.items;  // contains item ids 1, 2, 5
                  
                  coworkers.remove(2);
                  
                  contactRepo.items; // contains item ids 1, 2, 3, 4, 5 
                  friends.items;  // contains item ids 2, 4
                  coworkers.items;  // contains item ids 1, 5
                  
                  friends.remove(4);
                  
                  contactRepo.items; // contains item ids 1, 2, 3, 5 
                  friends.items;  // contains item ids 2
                  coworkers.items;  // contains item ids 1, 5
                  

                  注意 coworkers.remove(2) 如何沒有從 contactRepo 中刪除 id 2?這是因為它仍然被 friends.items 引用.但是,friends.remove(4) 會導致 id 4 從 contactRepo 中刪除,因為沒有其他集合引用它.

                  Notice how coworkers.remove(2) didn't remove id 2 from contactRepo? This is because it was still referenced from friends.items. However, friends.remove(4) causes id 4 to be removed from contactRepo, because no other collection is referring to it.

                  以上是我想做的.我確信有辦法通過跟蹤我自己的引用計數器等來做到這一點.但是如果有辦法使用 javascript 的內置引用管理來做到這一點,我想聽聽如何使用它.

                  The above is what I want to do. I'm sure there are ways I can do this by keeping track of my own reference counters and such. But if there is a way to do it using javascript's built-in reference management, I'd like to hear about how to use it.

                  推薦答案

                  不,不,不,不;是的,如果你真的需要計算引用,你將不得不手動進行.JS 沒有 this、GC 或弱引用的接口.

                  No, no, no, no; and yes, if you really need to count references you will have to do it manually. JS has no interface to this, GC, or weak references.

                  雖然您可以實現手動引用計數對象列表,但是否所有額外開銷(在性能方面,但更重要的是代碼復雜性)是否值得值得懷疑.

                  Whilst you could implement a manual reference-counted object list, it's questionable whether all the extra overhead (in performance terms but more importantly code complexity) is worth it.

                  在您的示例代碼中,忘記 Repository 似乎更簡單,為列表使用普通的 Array,并讓標準垃圾收集處理丟棄未使用的人.如果您需要獲取所有在用人員的列表,您只需 concat friendscoworkers 列表(并對它們進行排序/唯一化如果需要).

                  In your example code it would seem simpler to forget the Repository, use a plain Array for your lists, and let standard garbage collection take care of dropping unused people. If you needed to get a list of all people in use, you'd just concat the friends and coworkers lists (and sort/uniquify them if you needed to).

                  這篇關于如何獲取 javascript 對象引用或引用計數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                1. <tfoot id='vQtdC'></tfoot><legend id='vQtdC'><style id='vQtdC'><dir id='vQtdC'><q id='vQtdC'></q></dir></style></legend>
                    <tbody id='vQtdC'></tbody>

                    • <bdo id='vQtdC'></bdo><ul id='vQtdC'></ul>

                        1. <i id='vQtdC'><tr id='vQtdC'><dt id='vQtdC'><q id='vQtdC'><span id='vQtdC'><b id='vQtdC'><form id='vQtdC'><ins id='vQtdC'></ins><ul id='vQtdC'></ul><sub id='vQtdC'></sub></form><legend id='vQtdC'></legend><bdo id='vQtdC'><pre id='vQtdC'><center id='vQtdC'></center></pre></bdo></b><th id='vQtdC'></th></span></q></dt></tr></i><div class="3nilguv" id='vQtdC'><tfoot id='vQtdC'></tfoot><dl id='vQtdC'><fieldset id='vQtdC'></fieldset></dl></div>

                        2. <small id='vQtdC'></small><noframes id='vQtdC'>

                          • 主站蜘蛛池模板: 河南新乡德诚生产厂家主营震动筛,振动筛设备,筛机,塑料震动筛选机 | 物流公司电话|附近物流公司电话上门取货 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 泰安塞纳春天装饰公司【网站】 | PSI渗透压仪,TPS酸度计,美国CHAI PCR仪,渗透压仪厂家_价格,微生物快速检测仪-华泰和合(北京)商贸有限公司 | 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 3D全息投影_地面互动投影_360度立体投影_水幕灯光秀 | 广东银虎 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | 风电变桨伺服驱动器-风电偏航变桨系统-深圳众城卓越科技有限公司 | 嘉兴恒升声级计-湖南衡仪声级计-杭州爱华多功能声级计-上海邦沃仪器设备有限公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 汽车整车综合环境舱_军标砂尘_盐雾试验室试验箱-无锡苏南试验设备有限公司 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 高压包-点火器-高压发生器-点火变压器-江苏天网 | 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | 贵州水玻璃_-贵阳花溪闽兴水玻璃厂 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 便携式表面粗糙度仪-彩屏硬度计-分体式粗糙度仪-北京凯达科仪科技有限公司 | 空冷器|空气冷却器|空水冷却器-无锡赛迪森机械有限公司[官网] | 智能电表|预付费ic卡水电表|nb智能无线远传载波电表-福建百悦信息科技有限公司 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 主题班会网 - 安全教育主题班会,各类主题班会PPT模板 | 电脑知识|软件|系统|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 气动绞车,山东气动绞车,气动绞车厂家-烟台博海石油机械有限公司 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 活性炭-蜂窝-椰壳-柱状-粉状活性炭-河南唐达净水材料有限公司 | 低粘度纤维素|混凝土灌浆料|有机硅憎水粉|聚羧酸减水剂-南京斯泰宝 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 电表箱-浙江迈峰电力设备有限公司-电表箱专业制造商 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 电车线(用于供电给电车的输电线路)-百科 | 电动葫芦-河北悍象起重机械有限公司 |