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

stop global pollution and clean up methods/exports #12

Closed
wants to merge 3 commits into from
Closed

stop global pollution and clean up methods/exports #12

wants to merge 3 commits into from

Conversation

devsnek
Copy link

@devsnek devsnek commented Sep 4, 2017

No description provided.

i would fix that resolvable antipattern but gh web editor
index.js Outdated
@@ -1,8 +1,8 @@
/* globals fetch, Headers */
/* istanbul ignore next */
if (!process.browser) {
global.fetch = require('node-fetch')
global.Headers = global.fetch.Headers
var fetch = require('node-fetch')
Copy link

Choose a reason for hiding this comment

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

This will shadow the global fetch in browsers with undefined.

Copy link
Author

Choose a reason for hiding this comment

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

No it won't?

Copy link

Choose a reason for hiding this comment

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

the var fetch will in fact be hoisted, unconditionally, to the top of the scope, which since this is a module, won't be the global scope - it will be a function wrapper.

Copy link
Author

@devsnek devsnek Sep 4, 2017

Choose a reason for hiding this comment

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

...That code won't run in browsers, and in node it will hoist to the bootstrap wrapper function in module compile

Copy link

@ljharb ljharb Sep 5, 2017

Choose a reason for hiding this comment

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

var fetch runs unconditionally, even if it's in the if block - it's hoisted.

Which means that it's the same as:

var fetch;
if (!process.browser) {
  fetch = require('node-fetch');
}

module.exports.delete = (...args) => new R2().delete(...args)
const methods = ['get', 'put', 'post', 'head', 'patch', 'delete']
for (const method of methods) {
R2.prototype[method] = function(...args) {
Copy link

Choose a reason for hiding this comment

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

This is actually much slower, and also not equivalent (enumerability, for one) - these should stay as explicit prototype methods.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure that enumerability matters in this context, and the speed of attaching things to exports isn't really an issue in any situation.

Copy link

Choose a reason for hiding this comment

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

I'm referring to the speed of using the class in the first place, at runtime.

@gr2m
Copy link
Collaborator

gr2m commented Feb 27, 2018

Hey I think this PR is obsolete as per #38?

@devsnek devsnek closed this Feb 27, 2018
@devsnek devsnek deleted the patch-1 branch February 27, 2018 00:04
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.

3 participants