Hacker News new | ask | show | jobs
by carterehsmith 2292 days ago
That's what minifiers/uglifiers do? Perhaps OP did not want to use them.
1 comments

As far as I know minifiers don't do this since it changes semantics. Take this for example:

    // 1
    doSomething();
    console.log(Math.sin(2));

    // 2
    var s = Math.sin;
    doSomething();
    console.log(s(2));

Now, consider this definition for `doSomething()`:

    function doSomething() {
      Math.sin = x => x
    }

Now, the first prints 2 and the second snippet prints 0.9092.

Keeping track of globals to get around this is complicated. Even if you do, you can't always be sure the behaviour of the code isn't changing since `doSomething` could be defined in a module your minifier doesn't know about.