Skip to content

Incorrect JavaScript snippet#21150

Merged
Elchi3 merged 1 commit intomdn:mainfrom
eemanioui:patch-1
Sep 28, 2022
Merged

Incorrect JavaScript snippet#21150
Elchi3 merged 1 commit intomdn:mainfrom
eemanioui:patch-1

Conversation

@eemanioui
Copy link
Copy Markdown
Contributor

There's an issue with one of the suggested snippets on this page.

The first suggested snippet, under the bullet point below, is incorrect:

  1. Global variables of your script are, in fact, properties of window:
const global = { data: 0 }; 
alert(global === window.global); // displays "true" 

The above snippet displays an alert message with false and not true as suggested by the snippet, which is incorrect.

Variables declared with const and let don't become properties of the window object.

The only way to create a global variable that becomes a property of the window object, is to either declare a variable with var or initialize the variable without declaring it, like so:

Example 1: declaring variable with var

   var global = { data: 0 };
   alert(global === window.global); // displays "true"

Example 2: initializing a variable without declaration

   global = { data: 0 };
   alert(global === window.global); // displays "true"

There's an issue with one of the suggested snippets on this page. 


The first suggested snippet, under the below bullet point, is incorrect:
1. Global variables of your script are, in fact, properties of `window`:

   ```js
   const global = { data: 0 };
   alert(global === window.global); // displays "true"
   ```

Variables declared with `const`  and `let` don't become properties of the `window` object.
The only way to create a global variable that's a property of the `window` object, is to either declare a variable with `var` or initialize the variable without declaring it, like so:
Example 1: declaring variable with `var`
```js
   var global = { data: 0 };
   alert(global === window.global); // displays "true"
   ```

Example 2: initializing a variable without declaration
```js
   global = { data: 0 };
   alert(global === window.global); // displays "true"
   ```
@eemanioui eemanioui requested a review from a team as a code owner September 28, 2022 08:41
@eemanioui eemanioui requested review from Elchi3 and removed request for a team September 28, 2022 08:41
@github-actions github-actions bot added the Content:WebAPI Web API docs label Sep 28, 2022
@github-actions
Copy link
Copy Markdown
Contributor

Preview URLs

Flaws (1)

URL: /en-US/docs/Web/API/Window/window
Title: Window.window
Flaw count: 1

  • broken_links:
    • Can't resolve /en-US/docs/Mozilla/JavaScript_code_modules

Copy link
Copy Markdown
Member

@Elchi3 Elchi3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thank you!

The text also says: "The point of having the window property refer to the object itself, was likely to make it easy to refer to the global object. Otherwise, you'd have to do a manual let window = this; assignment at the top of your script."

I believe that should be var window = this; there as well?

Copy link
Copy Markdown
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was over-refactoring. Thank you and congratulations on your first contribution here—welcome aboard!

@Josh-Cena
Copy link
Copy Markdown
Member

I believe that should be var window = this; there as well?

No, that's not strictly necessary. Unless you absolutely want to make your custom window variable global, making it a script-level variable is usually good enough since it can be accessed anywhere within the current script (and HTML scripts do share scope), just not between scripts or modules of the same realm.

Copy link
Copy Markdown
Member

@Elchi3 Elchi3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Thanks for the explanation, @Josh-Cena 👍

@Elchi3 Elchi3 merged commit efd9c7f into mdn:main Sep 28, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Content:WebAPI Web API docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants