Home >Web Front-end >JS Tutorial >Simple example of closure implemented in javascript_javascript skills
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.