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

Comments

Projects
None yet
5 participants
@creatorrr
Copy link

creatorrr commented Apr 14, 2015

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

This comment has been minimized.

Copy link
Collaborator

jridgewell commented Apr 14, 2015

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

  var root = this || {};
@jashkenas

This comment has been minimized.

Copy link
Owner

jashkenas commented Apr 14, 2015

Underscore doesn't use strict mode.

@jridgewell

This comment has been minimized.

Copy link
Collaborator

jridgewell commented Apr 14, 2015

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

This comment has been minimized.

Copy link
Owner

jashkenas commented Apr 14, 2015

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

@jashkenas

This comment has been minimized.

Copy link
Owner

jashkenas commented Apr 14, 2015

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

Ugh.

Is this going to play nice with _.template?

@jridgewell

This comment has been minimized.

Copy link
Collaborator

jridgewell commented Apr 14, 2015

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

@jridgewell

This comment has been minimized.

Copy link
Collaborator

jridgewell commented Apr 14, 2015

@megawac

This comment has been minimized.

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

This comment has been minimized.

Copy link
Collaborator

megawac commented Apr 14, 2015

^ That would also add support for loading underscore in webworkers

@creatorrr

This comment has been minimized.

Copy link
Author

creatorrr commented Apr 14, 2015

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

@creatorrr

This comment has been minimized.

Copy link
Author

creatorrr commented Apr 14, 2015

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

@creatorrr

This comment has been minimized.

Copy link
Author

creatorrr commented Apr 16, 2015

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

@megawac

This comment has been minimized.

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

This comment has been minimized.

Copy link

fangmobile commented Nov 5, 2015

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
You can’t perform that action at this time.