The example in this article describes the closure implemented by javascript. Share it with everyone for your reference. The details are as follows:
var name = "The Window"; var obj = { name: "My Object", getNameFunc: function() { return function() { return this.name; }; } }; console.log( obj.getNameFunc()() );
var name = "The Window"; var obj = { name: "My Object", getNameFunc: function() { var that = this; return function() { return that.name; }; } }; console.log( obj.getNameFunc()() );
I hope this article will be helpful to everyone’s JavaScript programming design.