以下茉莉花测试适用于PhantomJS或Chrome,但不适用于MSIE 10。
describe("utility", function () {
var utility = {
// { A: true, B: true } will become 'AB'
CombineValues: function (splitValues) {
var combined = '';
for (item in splitValues) { // on IE, item is a function, not a string
if (splitValues[item]) // on IE, this returns false all the time
combined = combined + item;
}
return combined;
},
// 'AB' will become { A: true, B: true }
SplitValues: function (combined) {
var splitValues = {};
for (var i = 0; i < combined.length; i++) {
splitValues[combined.charAt(i)] = true;
}
return splitValues;
}
};
it('CombineValues(SplitValues()) should be idempotent', function () {
// on MSIE, result is '' instead of 'ABC'
expect(utility.CombineValues(utility.SplitValues('ABC'))).toBe('ABC');
});
});
我想念什么?
谢谢!
编辑:在IE上,该项显示为:
function item() {
[native code]
}
{
[prototype] : function() { [native code] } ,
prototype : {...}
}
在Internet Explorer中,window.item
是一个功能。显然是不可重写的。
通过for (item in …)
循环,您将隐式分配给该全局变量,该变量在草率模式下(无提示)失败。尝试添加一个"use strict";
,它将引发错误。
使用局部变量:
for (var item in splitValues) …
// ^^^
在严格模式下,分配给未声明的(在Chrome / Webkit中)全局变量也会失败,在草率模式下,您只是item
使用该代码创建了一个全局变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句