解決js函數(shù)閉包內(nèi)存泄露問(wèn)題的辦法
本文通過(guò)舉例,由淺入深的講解了解決js函數(shù)閉包內(nèi)存泄露問(wèn)題的辦法,分享給大家供大家參考,具體內(nèi)容如下
原始代碼:
function Cars(){ this.name = "Benz"; this.color = ["white","black"]; } Cars.prototype.sayColor = function(){ var outer = this; return function(){ return outer.color }; }; var instance = new Cars(); console.log(instance.sayColor()())
優(yōu)化后的代碼:
function Cars(){ this.name = "Benz"; this.color = ["white","black"]; } Cars.prototype.sayColor = function(){ var outerColor = this.color; //保存一個(gè)副本到變量中 return function(){ return outerColor; //應(yīng)用這個(gè)副本 }; outColor = null; //釋放內(nèi)存 }; var instance = new Cars(); console.log(instance.sayColor()())
稍微復(fù)雜一點(diǎn)的例子:
function inheritPrototype(subType,superType){ var prototype = Object(superType.prototype); prototype.constructor = subType; subType.prototype = prototype; } function Cars(){ this.name = "Benz"; this.color = ["white","black"]; } Cars.prototype.sayColor = function(){ var outer = this; return function(){ return outer.color; }; }; function Car(){ Cars.call(this); this.number = [321,32]; } inheritPrototype(Car,Cars); Car.prototype.sayNumber = function(){ var outer = this; return function(){ return function(){ return outer.number[outer.number.length - 1]; } }; }; var instance = new Car(); console.log(instance.sayNumber()()());
首先,該例子組合使用了構(gòu)造函數(shù)模式和原型模式創(chuàng)建Cars 對(duì)象,并用了寄生組合式繼承模式來(lái)創(chuàng)建Car 對(duì)象并從Cars 對(duì)象獲得屬性和方法的繼承;
其次,建立一個(gè)名為instance 的Car 對(duì)象的實(shí)例;instance 實(shí)例包含了sayColor 和sayNumber 兩種方法;
最后,兩種方法中,前者使用了一個(gè)閉包,后者使用了兩個(gè)閉包,并對(duì)其this 進(jìn)行修改使其能夠訪(fǎng)問(wèn)到this.color 和this.number。
這里存在內(nèi)存泄露問(wèn)題,優(yōu)化后的代碼如下:
function inheritPrototype(subType,superType){ var prototype = Object(superType.prototype); prototype.constructor = subType; subType.prototype = prototype; } function Cars(){ this.name = "Benz"; this.color = ["white","black"]; } Cars.prototype.sayColor = function(){ var outerColor = this.color; //這里 return function(){ return outerColor; //這里 }; this = null; //這里 }; function Car(){ Cars.call(this); this.number = [321,32]; } inheritPrototype(Car,Cars); Car.prototype.sayNumber = function(){ var outerNumber = this.number; //這里 return function(){ return function(){ return outerNumber[outerNumber.length - 1]; //這里 } }; this = null; //這里 }; var instance = new Car(); console.log(instance.sayNumber()()());
以上就是為大家分享的解決方法,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享,本文給出了2種防嵌入方法,需要的朋友可以參考下2014-12-12amd、cmd、esmodule、commonjs區(qū)別詳解
本文主要介紹了amd、cmd、esmodule、commonjs區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04javascript客戶(hù)端遍歷控件與獲取父容器對(duì)象示例代碼
本篇文章主要是對(duì)javascript客戶(hù)端遍歷控件與獲取父容器對(duì)象示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01微信小程序wx.getUserInfo授權(quán)獲取用戶(hù)信息(頭像、昵稱(chēng))的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序wx.getUserInfo授權(quán)獲取用戶(hù)信息(頭像、昵稱(chēng))的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08JavaScript 中 avalon綁定屬性總結(jié)
avalon是前端MVVM框架,在js中經(jīng)常會(huì)用到。這篇文章主要介紹了JavaScript 中 avalon綁定屬性總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10ECMAScript6函數(shù)默認(rèn)參數(shù)
這篇文章主要介紹了ECMAScript6函數(shù)默認(rèn)參數(shù)的相關(guān)資料,需要的朋友可以參考下2015-06-06JavaScript使用function定義對(duì)象并調(diào)用的方法
這篇文章主要介紹了JavaScript使用function定義對(duì)象并調(diào)用的方法,實(shí)例分析了javascript中function定義及使用對(duì)象與方法的相關(guān)技巧,需要的朋友可以參考下2015-03-03Bootstrap與KnockoutJs相結(jié)合實(shí)現(xiàn)分頁(yè)效果實(shí)例詳解
KnockoutJS是一個(gè)JavaScript實(shí)現(xiàn)的MVVM框架。接下來(lái)通過(guò)本文給大家介紹Bootstrap與KnockoutJs相結(jié)合實(shí)現(xiàn)分頁(yè)效果,對(duì)bootstrap knockoutjs相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-05-05淺談Webpack4 Tree Shaking 終極優(yōu)化指南
這篇文章主要介紹了淺談Webpack4 Tree Shaking 終極優(yōu)化指南,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11