How to solve memory leakage of javascript function closures _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.