0

I am trying to create an object like below

var x = function x() {};

Does this create an object at window level/global level.Is it good way of using it ?

1
  • 5
    Without the surrounding code there is no way to answer that. If you use window.x = function() {} then it will be global. Commented Mar 6, 2014 at 10:31

1 Answer 1

3

If this was written in the global name space.. meaning in a loaded script directly or in a script tag this will be global.

But if this was made inside, for example, a dom ready event or an onlad event handlers this wont be global.

Using window.variable makes it global even if you're inside any block.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer.Just to make sure i have used the above snippet like below .Will then be it is considered as global ? var x = function x() {}; x.set_Data = function(data){ this.data = data; }; function y(){ /*i guess x is global */ var f = x.data; alert(f); }
If it's directly under <script type="text/javascript"> var x = function x() {}; ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.