最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS: How can I prevent access to the global variables do? - Stack Overflow

programmeradmin3浏览0评论

Is to disable the global variable in the function that I want.

I would make like Expenssion of Adobe After Effect

example code :

function privateFunction(){
    return window;
}

then normally :

result : Window Object

but I want then :

result : undefined

What should I do?

please help me

I want blocking global variable access in function;

Is to disable the global variable in the function that I want.

I would make like Expenssion of Adobe After Effect

example code :

function privateFunction(){
    return window;
}

then normally :

result : Window Object

but I want then :

result : undefined

What should I do?

please help me

I want blocking global variable access in function;

Share Improve this question edited Aug 21, 2014 at 11:20 Jeong SangCheol asked Aug 21, 2014 at 11:14 Jeong SangCheolJeong SangCheol 1131 gold badge2 silver badges8 bronze badges 4
  • I'm afraid your question is really unclear. Can you explain a bit more? – T.J. Crowder Commented Aug 21, 2014 at 11:14
  • this may help you stackoverflow./questions/12118971/… – V31 Commented Aug 21, 2014 at 11:16
  • I want blocking global variable access in function.. – Jeong SangCheol Commented Aug 21, 2014 at 11:18
  • Possible copy of : stackoverflow./questions/23177039/….. – CODeeerrrrrrrr Commented Aug 21, 2014 at 11:23
Add a ment  | 

2 Answers 2

Reset to default 4

Shadow the global variable by a local one:

function privateFunction() {
    var window;
    return window; // not the Window, but undefined now
}

You need to wrap everything in a closure:

    (function() {
        var window = 'foo';
        function privateFunction(){
            return window;
        }
    
        console.log(privateFunction());
    })();

发布评论

评论列表(0)

  1. 暂无评论