[removed] access variables inside anonymous function from the outside

前端 未结 5 785
生来不讨喜
生来不讨喜 2020-12-06 16:17

Say I have this anonymous function:

(function(window){

 var private = \'private msg\';

 function sayit() {
   alert(private) // works
 }

 document.body.on         


        
5条回答
  •  借酒劲吻你
    2020-12-06 16:55

    They aren't intended as "private" variables; that's just how closures work. You can do the same thing in Perl and Python, at the very least, and probably a great many other languages with closures and lexical scoping.

    Debuggers like Firebug or Chrome Inspector can still show you the entire stack at any point (including closed-over variables), but other than that and without changing the original code, I think you're out of luck.

    Perhaps if you told us your actual problem... :)

提交回复
热议问题