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

remove ifs around function expressions #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pgarciacamou
Copy link

Function expressions are hoisted outside of block scopes, in other words, JavaScript will define the functions anyway. Thus, the HAS_UINT8ARRAY and HAS_BUFFERS if statements surrounding the functions do not play any role in this library.

JavaScript bundlers, such as Webpack, are assuming you are trying to declare the function inside the if's block scope only to be used inside the if statement. Like so:

if (HAS_BUFFERS) {
    var _bufferCharCodeAt = function (buf, offset) {
        return buf.readUInt8(offset);
    }
	// ...
}

// later in the code, webpack assumes "bufferCharCodeAt" is global:
something = bufferCharCodeAt; // note that there is no underscore.

This causes the library to error out with "bufferCharCodeAt is not defined" error. Which can be quickly mitigated by understanding scope in JavaScript and removing the unnecessary if statements from function expression declarations.

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.

1 participant