Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Function("return this") to get global? #2152

Closed
creatorrr opened this issue Apr 14, 2015 · 14 comments · Fixed by #2153
Closed

Use Function("return this") to get global? #2152

creatorrr opened this issue Apr 14, 2015 · 14 comments · Fixed by #2153

Comments

@creatorrr
Copy link

Underscore currently breaks in 'strict' mode while trying to access properties on the global object. The global object is being set using the (function(root) { ... })(this) pattern. However, in this case, root remains undefined and hence the issue.

Using the Function("return this")() pattern instead alleviates the issue, though arguably ugly. Thoughts?

Steps to reproduce: Import underscore in a babel-jest test harness. (uses strict mode)

@jridgewell
Copy link
Collaborator

Would be better to just create a root object on L12.

  var root = this || {};

@jashkenas
Copy link
Owner

Underscore doesn't use strict mode.

@jridgewell
Copy link
Collaborator

It's not an issue of Underscore using strict mode, but the new ES6 module loaders forcing strict mode.

@jashkenas jashkenas reopened this Apr 14, 2015
@jashkenas
Copy link
Owner

Seriously? Strict mode is enforced now in "real" ES6? Or is this just "polyfill" module loaders being zealous?

@jashkenas
Copy link
Owner

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-mode-code

Ugh.

Is this going to play nice with _.template?

@jridgewell
Copy link
Collaborator

It's not strict mode exactly. The ES6 spec says that this is undefined for module contexts.

@jridgewell
Copy link
Collaborator

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-mode-code

I stand corrected, it's always strict mode.

@megawac
Copy link
Collaborator

megawac commented Apr 14, 2015

Can we replace this with self? I'm pretty sure that should work with all browsers I've ever used

@megawac
Copy link
Collaborator

megawac commented Apr 14, 2015

^ That would also add support for loading underscore in webworkers

@creatorrr
Copy link
Author

@jashkenas jest doesn't play well with loading underscore either.

@creatorrr
Copy link
Author

Also, this issue affects Backbone as well. Relevant issue: jashkenas/backbone#3566

@creatorrr
Copy link
Author

@megawac self won't work in node or iojs.

@megawac
Copy link
Collaborator

megawac commented Apr 16, 2015

See pull
On Apr 16, 2015 8:13 AM, "Diwank Singh Tomer" notifications@github.com
wrote:

@megawac https://github.com/megawac self won't work in node or iojs.


Reply to this email directly or view it on GitHub
#2152 (comment)
.

@fangmobile
Copy link

ran into this when using babel-jest as scriptPreprocessor. Any idea? 1.8.3

  "dependencies": {
    "react-native": "^0.12.0",
    "react-native-animatable": "^0.1.0",
    "react-native-webintent": "^1.0.2"
  },
  "devDependencies": {
    "babel-jest": "*",
    "jest-cli": "^0.7.0"
  },
  "jest": {
    "collectCoverage": true,
    "rootDir": "./",        (I also used "", but same thing)
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
    "testFileExtensions": ["es6", "js"],
    "moduleFileExtensions": ["js", "json", "es6"]
  }
● Runtime Error
TypeError: /Users/yourname/Proj/ReactNativeExperiment/__tests__/test2-test.js: /Proj/src/components/assets/multiSelect.js:Proj/lib/underscore.js: Cannot read property '_' of undefined
    at Proj/lib/underscore.js:15:32
    at Object.<anonymous> (Proj/src/lib/underscore.js:1548:1)
    at Object.runContentWithLocalBindings (/Proj/node_modules/jest-cli/src/lib/utils.js:394:17)
    at Loader._execModule (/Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (/Prou/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at Loader.requireModuleOrMock /Prou/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
    at Object.<anonymous> (Proj/src/components/assets/multiSelect.js:9:45)
    at Object.runContentWithLocalBindings (/Proj/node_modules/jest-cli/src/lib/utils.js:394:17)
    at Loader._execModule (/Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at Loader.requireModuleOrMock (Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:875:19)
    at Object.<anonymous> (//Proj/__tests__/test2-test.js:4:19)
    at Object.runContentWithLocalBindings (/Proj/node_modules/jest-cli/src/lib/utils.js:394:17)
    at Loader._execModule /Proj/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:234:11)
    at Loader.requireModule (/Proj//node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:845:14)
    at jasmineTestRunner (/Proj/node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:264:16)
    at /Proj/node_modules/jest-cli/src/TestRunner.js:379:14

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

Successfully merging a pull request may close this issue.

5 participants