2

I'm from a C# Class(), fields, properties, namespace...world. And I just launched myself into javascript. So far I've been doing great. However one of my friends was looking at my code and asked me why I did this.

function Position (x, y) {
    this.X = x;
    this.Y = y; 
}

friend: "You've just over-rided Position..." me : "What Position?" friend: "Could be anything the browser is using." me : "I am only running this script - it works perfectly" friend: "Until it doesn't work."

Ok so... What? Turns out Position is Global .. but where and to what extent? My friend makes it sound like its Global to the entire browser. So my question is;

Is a javascript Global, Global to the Entire Browser? > The Window Only? > The Tab Only?? > how far does it go??

0

3 Answers 3

4

It is global to the current window. Don't worry about other tabs, windows or iframes. That being said what I think he is trying to state is a good principle in JavaScript namely

Don't clutter up the global namespace

meaning that whatever you make global should not be much and it should be very intentional.

JavaScript has function scope (not block scope) and so an easy way to get around this is by wrapping everything in an immediately invoked function expression.

;(function () {

     function Position (x, y){
          this.X = x;
          this.Y = y;
     }

     // use Position here

}());
Sign up to request clarification or add additional context in comments.

2 Comments

+1, I'd post an answer in these lines (but laziness :P). Also, as OP is starting with JS, here are some reference links to help out: Immediately-Invoked Function Expression (IIFE), JavaScript Module Pattern: In-Depth
hrmm interesting.. just diving into jRequire - and learning how to make 'modules'. The Position() was used to create many positions for drawable objects, I can declare them in a easy to read area that way if I needed to tweak a position I could tweak it there and not have to hunt for it elsewhere...
2

When JavaScript is running in a browser, a global variable is a property of the window object. Therefore, it is global to the current window only, not to other browser windows, tabs, frames, or iframes. Each one of those has its own window object.

There is no Position global built into JavaScript. Your friend's concern is probably that some other piece of code you include on your page may also define that same global. So one of those definitions (whichever is defined later) will overwrite the other.

4 Comments

each iframe also has its own window, and the deprecated frames too.
"There is no Position global built into JavaScript" - The OP defined a (global) Position function...
@Ian - yes, and as long as that is the only global Position on the page that's fine. Of course if some other code defines that same global there will be trouble!
@MichaelGeary Ahh sorry, I see what you're saying. I think I was confused with how things were said in the conversation with the OP's friend
0

Position is accessible from everywhere in the browser window. In web terms, I believe the browser window is the highest scope you can go.

What your friend is probably saying is, if someone already defined an object named Position that exists in the global scope, your code just changed that behavior. In general, you will want to protect the global scope from any of your custom methods by namespacing.

Comments

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.