本文實例講述了javascript實現的閉包。分享給大家供大家參考。具體如下:
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()() );
希望本文所述對大家的javascript程式設計有所幫助。