Skip to content

Don't replace (function(){})(); with !function(){}(); unless the return value isn't used #354

Closed

Description

@Daniel-Hug

I use UglifyJS to compress and mangle my JS and stick it in a bookmarklet.

This is the bookmarklet's JS (the anon function is to make sure it evaluates to undefined):

(function() {
  /*bookmarklet stuff*/
})();

This is the JS after compression (notice the exclamation point):

!function(){/*bookmarklet stuff*/}();

The exclamation point negates the return value of the anon function so instead of evaluating to undefined, it will evaluate to true. Then when I prepend the javascript: "protocol" the bookmarklet instead navigates to a blank page displaying true.

IMO UglifyJS shouldn't negate anonymous functions if the return value is used or in this case if the function is the root function.

Activity

  1. michaelficarra commented on Nov 21, 2013

    @michaelficarra
    Contributor
  2. rvanvelzen commented on Nov 21, 2013

    @rvanvelzen
    Collaborator

    I would say adding -c negate_iife=false would fix this.

  3. mishoo commented on Nov 21, 2013

    @mishoo
    Owner

    In general I think we prefer the current behaviour, and when you're building for a bookmarklet you can pass -c negate-iife=false as @rvanvelzen suggested.

  4. added 4 commits that reference this issue on Mar 1, 2017
    dfb2d43
    91f29e8
    f01c0c4
    07accd2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions