我有以下的javascript它工作在調試正常,但在生產中由於不當的微小失敗:JavaScript生成不正確的功能縮小,是我的代碼壞?
function buildNotification(config) {
var notificationWrapper = $('<div>', {
'id': _.uniqueId('notification_'),
'class': 'notificationWrapper'
});
var notification = $('<div>', {
'class': 'notification ui-widget ui-state-default'
});
notification.addClass(config.notificationClass);
notification.appendTo(notificationWrapper);
var notificationList = $('<ul/>', {
'class': 'notificationList'
}).appendTo(notification);
// THIS CODE IS IMPROPERLY MINIFIED:
$.each(config.messages, function() {
$('<li/>', {
html: this
}).appendTo(notificationList);
});
return notificationWrapper;
}
在哪裏設置列表項的HTML標記的cuplrit是基於配置。
縮小的加價樣子:
function g(a) {
var b = $("<div>", { id: _.uniqueId("notification_"), "class": "notificationWrapper" }), c = $("<div>", { "class": "notification ui-widget ui-state-default" });
c.addClass(a.notificationClass);
c.appendTo(b);
var d = $("<ul/>", { "class": "notificationList" }).appendTo(c);
$.each(a.messages, function() { $("<li/>", { html: this }).appendTo(d); });
return b;
}
而這裏的錯誤消息我收到:
可能有人砸我的一些知識?我在做一些不好的練習嗎?我通過JSHint運行代碼,沒有任何抱怨,並且我還在文件的頂部使用'strict'。
更新:使用Google Closure進行縮小時遇到同樣的問題。它生成的代碼是:
function g(a) {
var b = $("<div>", { id: _.uniqueId("notification_"), "class": "notificationWrapper" }), c = $("<div>", { "class": "notification ui-widget ui-state-default" });
c.addClass(a.notificationClass);
c.appendTo(b);
var d = $("<ul/>", { "class": "notificationList" }).appendTo(c);
$.each(a.messages, function() { $("<li/>", { html: this }).appendTo(d); });
return b;
}
這與YUI Compressor相同。
更新2:http://jscompress.com/如果我使用這個軟件壓縮我的文件,它的工作原理。
生成的代碼:
function r(e) {
var t = $("<div>", { id: _.uniqueId("notification_"), "class": "notificationWrapper" });
var n = $("<div>", { "class": "notification ui-widget ui-state-default" });
n.addClass(e.notificationClass);
n.appendTo(t);
var i = $("<ul/>", { "class": "notificationList" }).appendTo(n);
$.each(e.messages, function() { $("<li/>", { html: this }).appendTo(i); });
return t;
}
嘗試用不同的工具來縮小第一,像谷歌關閉:HTTP://closure-compiler.appspot。com/home – Evgeny
它是現有項目中包含數千個文件的一部分。目前我不太喜歡改變縮小工具。我會比較縮小的結果,看看是否有差異。 –
@EugeneXa我遇到與Google Closure相同的錯誤。 –