原来的代码, 是一段把日期格式化的函数(抄的另一位博友的),在debug下跑没有问题,但bundle优化了以后就跑不过去了。
var selectedDay = news.date2str(ev.date,"yyyy-MM-dd");
date2str: function(x, y) {
var realDate = { M:x.getMonth() + 1, d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s:x.getSeconds() };
var kk =y.replace(/(M+|d+|h+|m+|s+)/g, function (eachV) {
return((eachV.length > 1 ? "0" : "") + eval('realDate.' +eachV.slice(-1))).slice(-2);
});
return kk.replace(/(y+)/g, function(ss) { return x.getFullYear().toString().slice(-ss.length); });
},
原因:
var realDate = { M:x.getMonth() + 1, d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s:x.getSeconds() };
被bundle改成了
vars = { M: x.getMonth() + 1, d:x.getDate(), h: x.getHours(), m: x.getMinutes(), s: x.getSeconds() };
但相应的
eval('realDate.' + eachV.slice(-1))).slice(-2);中的realDate 没有改。 Eval会crash
解决方案:(一定不是最优的,希望有人告诉我怎么做)
date2str: function (x, y) {
// 这里有个workaround,realDate 需要放在function (eachV)内部,否则bundle会把它名字改掉,但相应的eval函数里的字符串却不会改
var kk =y.replace(/(M+|d+|h+|m+|s+)/g, function (eachV) {
var realDate = { M:x.getMonth() + 1, d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s:x.getSeconds() };
return ((eachV.length > 1 ?"0" : "") + eval('realDate.' + eachV.slice(-1))).slice(-2);
});
return kk.replace(/(y+)/g, function(ss) { return x.getFullYear().toString().slice(-ss.length); });
},