久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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="qwawimqqmiuu" 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="qwawimqqmiuu" 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="qwawimqqmiuu" id='vQtdC'><tfoot id='vQtdC'></tfoot><dl id='vQtdC'><fieldset id='vQtdC'></fieldset></dl></div>

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

                          • 主站蜘蛛池模板: 国产视频一区二区 | 蜜桃综合在线 | 久久久久久成人 | 国产成人高清成人av片在线看 | 午夜a级理论片915影院 | 男人的天堂在线视频 | 精品1区2区3区 | 草草视频在线播放 | 久久国产一区 | 91精品国产91久久久久久 | 国产高清精品一区二区三区 | 成年人网站免费 | 精品国产一区二区久久 | 亚洲精品www久久久久久广东 | www.99精品 | 精品在线播放 | 色综合天天天天做夜夜夜夜做 | 91久久久久久久 | 成人不卡 | www.欧美| 成人一区二区三区视频 | 不卡在线一区 | 毛片网站在线观看视频 | 欧美久久久久久久久 | 国产精品毛片一区二区三区 | 国产a区 | 国产精品一区二区三区久久 | 国产精品久久久久久久免费大片 | 久久久精品网站 | 亚洲精品乱码久久久久久蜜桃91 | 男人天堂久久久 | www日韩 | 欧美另类视频 | 美女视频一区二区三区 | 特一级黄色毛片 | 久久精品91久久久久久再现 | 免费福利视频一区二区三区 | 久久精彩视频 | 狠狠操婷婷 | 欧美日韩一本 | 亚洲色图50p |