question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"this" should reference the window object

See original GitHub issue

This a 🐛 bug report.

“this” within imported scripts references an empty object rather than the window object.

🎛 Configuration

parcel index.html

(That’s it! That’s why I love parcel!!! 😃)

🤔 Expected Behavior

The window object should be available as “this” to scripts imported via the script tag.

😯 Current Behavior

“this” references an empty object instead.

💁 Possible Solution

I haven’t had a chance to go through the source code, but I imagine that imported code is being run within with a context somewhere. We’re passing in “{}” as the context where we should pass in the window object.

Alternatively, we might pass in a parcel-specific attribute to HTML tags that would direct parcel not to run code in a different context, but execute it untouched?

<script parcel-untouched src="./external-script"></script>

🔦 Context

I’m developing packages with rollup. Creating modules that attach themselves to the window object appears to need “this” to reference the window.

This might be consider an issue with rollup, or maybe even just my configuration, but it seems entirely legitimate to develop other libraries in this way.

💻 Code Sample

external-script.js

console.log(this);

index.html (partial)

<script src="./external-script.js"></script>
<!-- logs "{}" -->
<script>
    console.log(this);
</script>
<!-- logs window object as expected-->

🌍 Your Environment

Software Version(s)
Parcel 1.6.1
Node 9.5.0
npm/Yarn npm 5.6.0
Operating System Mac 10.13.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mischniccommented, Feb 18, 2018

I don’t know if that would be correct, some sources:

Babel assumes that all input code is an ES2015 module. ES2015 modules are implicitly strict mode so this means that top-level this is not window in the browser nor is it exports in node. (https://babeljs.io/faq#why-is-this-being-remapped-to-undefined)

Module code is always strict mode code. (http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code)

That means, among other things, that in browsers it’s no longer possible to reference the window object through this inside a strict mode function. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)

0reactions
github-actions[bot]commented, Aug 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the purpose of referencing the window object in ...
One situation in which it helps to be explicit is that it will be immediately clear inside a function that you intend to...
Read more >
Window Object - W3Schools
The window object represents an open window in a browser. ... also The Navigator object. opener, Returns a reference to the window that...
Read more >
Window - Web APIs - MDN Web Docs - Mozilla
desktop desktop Chrome Edge Window Full support. Chrome1. Toggle history Full support. Edge12. To... DOMContentLoaded event Full support. Chrome1. Toggle history Full support. Edge12. To...
Read more >
Every known way to get references to windows, in javascript
You can access it by name by referencing window.frames['myIframeName'] or window.frames.myIframeName — or even just window.myIframeName . These ...
Read more >
Angular: How do I get a reference to the window object?
Referencing global browser objects like document or window directly from within your code is possible, but not encouraged and considered bad ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found