top | item 22531448

(no title)

carterehsmith | 6 years ago

That's what minifiers/uglifiers do? Perhaps OP did not want to use them.

discuss

order

aylmao|6 years ago

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.