This article provides examples to illustrate how to solve the memory leakage problem of js function closures. If you are interested, you can refer to the examples in this article, I have explained how to solve the memory leakage problem of js function closures in a simple way and shared it with you for your reference. The specific content is as follows:
Original code:
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()())
Optimized Code:
Function Cars () {this. name = "Benz"; this. color = ["white", "black"];} Cars. prototype. sayColor = function () {var outerColor = this. color; // save a copy to the variable return function () {return outerColor; // apply this copy}; outColor = null; // release the memory }; var instance = new Cars (); console. log (instance. sayColor ()())
A slightly more complex example:
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()()());
First, this example combines the constructor mode and the prototype mode to create the Cars object, and uses the parasitic combined inheritance mode to create the Car object and obtain the inheritance of attributes and methods from the Cars object;
Create an instance of the Car object named instance. The instance contains two methods: sayColor and sayNumber;
Finally, in the two methods, the former uses a closure, the latter uses two closures, and modifies this so that it can access this. color and this. number.
The memory leakage problem exists. The optimized code is as follows:
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; // here}; this = null; // here}; function Car () {Cars. call (this); this. number = [321,32];} inheritPrototype (Car, Cars); Car. prototype. sayNumber = function () {var outerNumber = this. number; // return function () {return outerNumber [outerNumber. length-1]; // here }}; this = null; // here}; var instance = new Car (); console. log (instance. sayNumber ()()());
The above is the solution shared for you and I hope it will be helpful for your learning.