Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an example of JavaScript code whose semantics would be changed by minification?

I have reports - no source available, I'm afraid - of a web application that doesn't work on devices using some mobile data networks, because the network operators are running a non-transparent proxy that is compressing images and minifying JavaScript, and the minification is actually breaking the code.

I'm curious as to whether anyone has an example (i.e. a code snippet plus a minification technique) whereby sloppy JS code combined with aggressive minification could actually change the meaning of the code? I suspect such a combination is possible but can't think of - or find - any examples. Anyone got a good example, or a proof to the contrary?

like image 363
Dylan Beattie Avatar asked Aug 05 '14 09:08

Dylan Beattie


People also ask

What is JavaScript minification?

JavaScript Minification is the process of reducing the size of JavaScript files. To reduce the size, the comments, extra white spaces, and new line characters are removed from the script and the variable names are replaced with shorter variable names.

What are semantic elements and non-semantic elements in HTML?

A semantic element clearly describes its meaning to both the browser and the developer. Examples of non-semantic elements: <div> and <span> - Tells nothing about its content. Examples of semantic elements: <form>, <table>, and <article> - Clearly defines its content. Semantic Elements in HTML

What can JavaScript do?

What can JavaScript do? JavaScript statements are commands to the browser JavaScript code is a sequence of statements JavaScript statements are separated with semicolon Multiple statement on one line is allowed JavaScript statements can be grouped together in code blocks You can break a code line after an operator or a comma.

What are the advantages and disadvantages of JS minification?

Advantages of Minification As minification reduces the size of JS file it reduces the download time and the web page loads much faster. It reduces the bandwidth consumption of site. It improves the script execution time also.


1 Answers

consider the following code:

function DoStuff(thingA, ThingB){
    var thingC = thingA + ThingB;
    return thingC;
}

var stuffingC = eval("DoStuff(stuffingA, stuffingB)");

minifiers sometimes shorten variable or function names:

function DS(A, B){return A+B;}

var C= eval("DoStuff(stuffingA, stuffingB)");

In this case, your code would break because the eval'd string isn't changed to account for the changed name of your function.

this is a basic example, but this is often what happens: you have some sort of reflection or evaluation of a string variable that refers to a minified piece of code with the pre-minification name, but isn't changed to account for this minified nature.

like image 79
Nzall Avatar answered Oct 06 '22 15:10

Nzall