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

<legend id='49dPn'><style id='49dPn'><dir id='49dPn'><q id='49dPn'></q></dir></style></legend>

    1. <tfoot id='49dPn'></tfoot>

    2. <small id='49dPn'></small><noframes id='49dPn'>

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

        • <bdo id='49dPn'></bdo><ul id='49dPn'></ul>

        Javascript中同一變量上的“new"沒有“delete&qu

        `new` without `delete` on same variable in Javascript(Javascript中同一變量上的“new沒有“delete)

        <i id='czDgL'><tr id='czDgL'><dt id='czDgL'><q id='czDgL'><span id='czDgL'><b id='czDgL'><form id='czDgL'><ins id='czDgL'></ins><ul id='czDgL'></ul><sub id='czDgL'></sub></form><legend id='czDgL'></legend><bdo id='czDgL'><pre id='czDgL'><center id='czDgL'></center></pre></bdo></b><th id='czDgL'></th></span></q></dt></tr></i><div class="becm8rs" id='czDgL'><tfoot id='czDgL'></tfoot><dl id='czDgL'><fieldset id='czDgL'></fieldset></dl></div>
            <tbody id='czDgL'></tbody>
            <bdo id='czDgL'></bdo><ul id='czDgL'></ul>
            <tfoot id='czDgL'></tfoot>
          • <small id='czDgL'></small><noframes id='czDgL'>

              <legend id='czDgL'><style id='czDgL'><dir id='czDgL'><q id='czDgL'></q></dir></style></legend>

                  本文介紹了Javascript中同一變量上的“new"沒有“delete"的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  這樣可以嗎?:

                  function mygetTime()
                  {
                      var d = new Date();
                      return(d.getTime());
                  }
                  
                  function wasteSomeMemory()
                  {
                      var temp;
                      for(var count = 0; count < 1000000; count += 1)
                      {
                          temp = mygetTime();
                      }
                  }
                  

                  調用 wasteSomeMemory() 會導致內存泄漏嗎?

                  Will calling wasteSomeMemory() cause a memory leak?

                  這個呢:

                  function wasteSomeMemory2()
                  {
                      var temp;
                      for(var count = 0; count < 1000000; count += 1)
                      {
                          temp = new Date();
                      }
                  }
                  

                  調用 wasteSomeMemory2() 會導致內存泄漏嗎?我應該在 for 循環的末尾使用 delete temp; 嗎?

                  Will calling wasteSomeMemory2() cause a memory leak? Should I use delete temp; at the end of the for-loop?

                  function wasteSomeMemory2()
                  {
                      var temp;
                      for(var count = 0; count < 1000000; count += 1)
                      {
                          temp = new Date();
                          delete temp;
                      }
                  }
                  

                  推薦答案

                  newdelete 在 JavaScript 中彼此沒有任何關系(盡管它們與其他語言中完全不同的結構).不要擔心在沒有明確清理的情況下創建對象(new),這是垃圾收集器的工作.

                  new and delete have nothing whatsoever to do with each other in JavaScript (despite their confusing similarity to completely different constructs in other languages). Don't worry about creating objects (new) without explicitly cleaning them up, that's the garbage collector's job.

                  new 用于通過構造函數創建對象.另一方面,delete 用于從對象中刪除屬性.它與從內存中刪除對象無關,除了作為副作用(例如,如果對該對象的唯一未完成引用來自您刪除的屬性).

                  new is for creating objects via constructor functions. delete, on the other hand, is for removing properties from objects. It has nothing to do with removing an object from memory, other than as a side effect (e.g., if the only outstanding reference to that object was from the property that you removed).

                  delete的正確使用示例:

                  var obj = {};
                  obj.foo = "bar"; // Now `obj` has a property called `foo`
                  delete obj.foo;  // Now it doesn't
                  

                  您的 getmyTime 功能非常好.Date 對象將有資格在函數返回后立即被回收(是否被回收完全取決于實現).它不會導致內存泄漏,除非有錯誤的實現.

                  Your getmyTime function is perfectly fine. The Date object will become eligible to be reclaimed immediately upon function return (whether it is reclaimed is completely down to the implementation). It does not cause a memory leak, except on a buggy implementation.

                  您的 wasteSomeMemory2 同樣不會導致內存泄漏,實際上您不能調用 delete temp; —你只能刪除屬性,不能刪除變量.

                  Your wasteSomeMemory2 similarly doesn't cause a memory leak, and in fact you can't call delete temp; — you can only delete properties, not vars.

                  次您必須幫助垃圾收集器,但這些通常(根據我的經驗)與對象屬性無關,因此不涉及 delete.它們只有在您創建函數實例時才會真正出現(如果您正在設置事件處理程序或計時器函數等,這很常見).例如,考慮:

                  There are times when you have to help the garbage collector out, but those usually don't (in my experience) have to do with object properties and so don't involve delete. They only really come up when you're creating function instances (which is fairly often, if you're setting up event handlers or timer functions, etc.). For instance, consider:

                  function foo() {
                      var listOfThings = /* ...get a list of things... */;
                  
                      // ...do something with `listOfThings`...
                  
                      setInterval(function() {
                         // ...do something that *doesn't* need `listOfThings`...
                      }, 1000);
                  }
                  

                  因為您通過 setInterval 分配給計時器的匿名函數將在函數調用中繼續存在,它會保持對該函數調用期間范圍內所有內容的實時引用(無論它是否使用它)或不).這會將 listOfThings 指向的事物列表保存在內存中.如果計時器功能不需要該列表,那是一個問題.如果您知道該函數不需要它,則可以通過分配 undefinednull 或其他任何內容來釋放 listOfThings 指向的列表listOfThings 完成后:

                  Because your anonymous function you've assigned to a timer via setInterval will survive the function call, it keeps a live reference to everything that was in-scope during that function call (whether it uses it or not). This keeps the list of things that listOfThings points to in memory. If the timer function doesn't need that list, that's a concern. You can release the list that listOfThings points to if you know that the function doesn't need it, by assigning undefined or null or whatever to listOfThings when you're done with it:

                  function foo() {
                  
                      var listOfThings = /* ...get a list of things... */;
                  
                      // ...do something with `listOfThings`...
                  
                      listOfThings = undefined; // Done with it           <== The new bit
                  
                      setInterval(function() {
                         // ...do something that *doesn't* need `listOfThings`...
                      }, 1000);
                  }
                  

                  事件處理函數等也是如此.每當您創建一個函數時,它都會關閉"(保持對它的定義范圍內的任何內容的實時引用).因此,如果您不需要這些東西,您可以通過清除對它們的引用來確保它們不會保留在內存中.(更多:閉包并不復雜)

                  The same is true for event handler functions, etc. Whenever you create a function, it "closes over" (keeps a live reference to) anything in scope where it was defined. So if you don't need those things, you can ensure they're not kept in memory by clearing the references to them. (More: Closures are not complicated)

                  這篇關于Javascript中同一變量上的“new"沒有“delete"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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. <i id='8GXZy'><tr id='8GXZy'><dt id='8GXZy'><q id='8GXZy'><span id='8GXZy'><b id='8GXZy'><form id='8GXZy'><ins id='8GXZy'></ins><ul id='8GXZy'></ul><sub id='8GXZy'></sub></form><legend id='8GXZy'></legend><bdo id='8GXZy'><pre id='8GXZy'><center id='8GXZy'></center></pre></bdo></b><th id='8GXZy'></th></span></q></dt></tr></i><div class="gzkxhmn" id='8GXZy'><tfoot id='8GXZy'></tfoot><dl id='8GXZy'><fieldset id='8GXZy'></fieldset></dl></div>
                        <bdo id='8GXZy'></bdo><ul id='8GXZy'></ul>

                          <small id='8GXZy'></small><noframes id='8GXZy'>

                        1. <tfoot id='8GXZy'></tfoot>
                              <tbody id='8GXZy'></tbody>

                          • <legend id='8GXZy'><style id='8GXZy'><dir id='8GXZy'><q id='8GXZy'></q></dir></style></legend>
                            主站蜘蛛池模板: 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 行星齿轮减速机,减速机厂家,山东减速机-淄博兴江机械制造 | crm客户关系管理系统,销售管理系统,crm系统,在线crm,移动crm系统 - 爱客crm | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | 地图标注-手机导航电子地图如何标注-房地产商场地图标记【DiTuBiaoZhu.net】 | 万博士范文网-您身边的范文参考网站Vanbs.com | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 包装机_厂家_价格-山东包装机有限公司| TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 电力测功机,电涡流测功机,磁粉制动器,南通远辰曳引机测试台 | 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 台式核磁共振仪,玻璃软化点测定仪,旋转高温粘度计,测温锥和测温块-上海麟文仪器 | 传爱自考网_传爱自学考试网 | 耐腐蚀泵,耐腐蚀真空泵,玻璃钢真空泵-淄博华舜耐腐蚀真空泵有限公司 | 护栏打桩机-打桩机厂家-恒新重工 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 二手回收公司_销毁处理公司_设备回收公司-找回收信息网 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 钢衬玻璃厂家,钢衬玻璃管道 -山东东兴扬防腐设备有限公司 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 恒温恒湿试验箱厂家-高低温试验箱维修价格_东莞环仪仪器_东莞环仪仪器 | 电脑刺绣_绣花厂家_绣花章仔_织唛厂家-[源欣刺绣]潮牌刺绣打版定制绣花加工厂家 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 珠宝展柜-玻璃精品展柜-首饰珠宝展示柜定制-鸿钛展柜厂家 | 光伏支架成型设备-光伏钢边框设备-光伏设备厂家 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 无锡装修装潢公司,口碑好的装饰装修公司-无锡索美装饰设计工程有限公司 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 |