When I compress the javascript, I found that there are many problem in our codes.
Although I had pay some attention to the code problem, there still exist many problems. These problems are too subtile to absorb our attention.
for example, a function can be declared in two ways:
1.function test() {
alert('this is a test');
}
function test2() {
alert('this is a test2');
}
function test3() {
alert('this is a test3');
}
2. var test = function() {
alert('this is a test');
}
var test2 = function() {
alert('this is a test2');
}
var test3 = function() {
alert('this is a test3');
}
When to do the simple compression, we want to remove all the unnecessary chars from the codes. so all the lines of codes will be put together in one line. That makes problem.
In case 1, there will be no problem.However, thre are problems in case 2.
The script interpreter will says: missing ";" before .....
What's the problem?
if we just put the statement like this, then you will found the problem.
var test = 'aaa' var test2='bbb' var test3='ccc'
Got it. ";" (semicolon) need at the end of each statement.
The same reason for case 2.
The codes should be written like this:
var test = function() { alert('this is a test');}; var test2 = function() { alert('this is a test2');}; var test3 = function() { alert('this is a test3');};
The same rule for prototype statement:
String.prototype.trim = function() {
};
String.prototype.html = function() {
};
In one word, the ";" should not be missed at the end of Assignment statement.