Skip to content

Ensure global setTimeout reference is captured in require.js#1402

Closed
platinumazure wants to merge 4 commits into
requirejs:masterfrom
platinumazure:close-over-setTimeout
Closed

Ensure global setTimeout reference is captured in require.js#1402
platinumazure wants to merge 4 commits into
requirejs:masterfrom
platinumazure:close-over-setTimeout

Conversation

@platinumazure

Copy link
Copy Markdown
Contributor

Ensuring global setTimeout reference is captured in require.js so that clobbering setTimeout does not break everything.

This is useful/necessary when using RequireJS with a unit test framework that simulates setTimeout (for example sinon.js). Since RequireJS relies on the default behavior of setTimeout and is a library which ideally should function the same way even when other frameworks are involved, it seems worthwhile to close over the default setTimeout function.

I've also added a unit test file to capture this-- please let me know if that can be improved.

@jrburke

jrburke commented Aug 20, 2015

Copy link
Copy Markdown
Member

I generally prefer to avoid using the "global" object, or whatever "this" is set to when requirejs evaluates, as it can be a flexible thing, assumes too much about the environment, where relying on scope feels more composable to me.

In "use strict" code "this" can be undefined too. The "global" use in requirejs right now is only around shim support which is a secondary, bridging feature, not something core to the loader, so I am OK if the user wants "use strict" code to be true for them to use the loader without shim exports support.

So I am not too eager to do this change. I also want to be sure the issue is not something else, as sinon and requirejs have both been in use for a while. Going to close for now, but feel free to continue discussion here and we can reopen later if needed.

@jrburke jrburke closed this Aug 20, 2015
@platinumazure

Copy link
Copy Markdown
Contributor Author

There is some precedent in using a "global" variable and accessing setTimeout from there, which works even in Node.js and other environments. See [qunit.js][1](line 21 is where setTimeout is captured, and about 2/3 of the way down is where the closure is invoked with a global object).

Can you enumerate a specific example of where setTimeout is only available as a global variable, but not available on a "global object"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants