Rhino 1.5 R5在查找数组差异时未正确评估JavaScript
Rhino 1.5 R5 not evaluating JavaScript correctly when finding array differences
我遇到了一些JavaScript问题,该问题正在使用Rhino 1.5 R5的应用程序中进行评估(Rhino 1.5 release 5 2004 03 25)。我无法独立更新Rhino。
我有两个数组:原始数据(pastedArr)和一个只有有效值的数组(validatedArr),我正在努力找出差异("坏"值)。我在网上找到了一个函数来帮助(sym),我不得不为Each制作原型,但它在jsfiddle中运行得很好(https://jsfiddle.net/zr3abc0c/)。
在应用程序中,使用Rhino,它不会返回整数元素(即使它们存储为字符串),就像下面的脚本和fiddle链接中一样,该链接在jsfiddle上返回["33"],但在我的应用程序中为null。如果参数0是["33"、"1A"、"ABC"],则在应用程序中只返回["1A"answers"ABC"。
我不知道这些"integer"字符串元素在哪里失败了。
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
// 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
// 4. If isCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
function sym( /* pass one or more arrays here */ ) {
var ans = [],
cnts = {},
currentMap;
//count all items in the array
for (var i = 0; i < arguments.length; i++) {
currentMap = {};
arguments[i].forEach(function(item) {
// if we haven't already counted this item in this array
if (!currentMap.hasOwnProperty(item)) {
if (cnts.hasOwnProperty(item)) {
// increase cnt
++cnts[item].cnt;
} else {
// initalize cnt and value
cnts[item] = {
cnt: 1,
val: item
};
}
}
// keep track of whethere we've already counted this item in this array
currentMap[item] = true;
});
}
// output all items that have a cnt of 1
for (var item in cnts) {
if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
ans.push(cnts[item].val);
}
}
return ans;
}
function sort_uniq_fast(a) {
var seen = {};
var out = [];
var len = a.length;
var j = 0;
for (var i = 0; i < len; i++) {
var item = a[i];
if (seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out.sort();
}
function getBadCodes(pastedArr, validatedArr) {
var result = sym(pastedArr, validatedArr);
return sort_uniq_fast(result);
}
var parameter0 = ["33"];
var parameter1 = new Array();
alert(getBadCodes(parameter0,parameter1));
我没有Rhino 1.5R5,但Rhino将整数属性视为特殊属性,因为它们在Array中使用。我推测Rhino 1.5R5中的这种处理方式存在错误。
我建议对项目进行预处理以进行比较,并对结果进行后处理,就像我在这个答案中所做的那样(请参阅前缀和取消前缀)。
让我们知道它是否有效!(我会相应地更新答案。)
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
// 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
// 4. If isCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
function sym( /* pass one or more arrays here */ ) {
var ans = [],
cnts = {},
currentMap;
//count all items in the array
for (var i = 0; i < arguments.length; i++) {
currentMap = {};
arguments[i].forEach(function(item) {
// if we haven't already counted this item in this array
if (!currentMap.hasOwnProperty(item)) {
if (cnts.hasOwnProperty(item)) {
// increase cnt
++cnts[item].cnt;
} else {
// initalize cnt and value
cnts[item] = {
cnt: 1,
val: item
};
}
}
// keep track of whethere we've already counted this item in this array
currentMap[item] = true;
});
}
// output all items that have a cnt of 1
for (var item in cnts) {
if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
ans.push(cnts[item].val);
}
}
return ans;
}
function sort_uniq_fast(a) {
var seen = {};
var out = [];
var len = a.length;
var j = 0;
for (var i = 0; i < len; i++) {
var item = a[i];
if (seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out.sort();
}
function prefix(array) {
var rv = [];
for (var i=0; i<array.length; i++) {
rv[i] = "A" + array[i];
}
return rv;
}
function unprefix(array) {
var rv = [];
for (var i=0; i<array.length; i++) {
rv[i] = array[i].substring(1);
}
return rv;
}
function getBadCodes(pastedArr, validatedArr) {
var result = unprefix(sym(prefix(pastedArr), prefix(validatedArr)));
return sort_uniq_fast(result);
}
var parameter0 = ["33"];
var parameter1 = new Array();
alert(getBadCodes(parameter0,parameter1));
相关文章:
- 下载javascript文件,但不要立即对其进行评估
- 在WebView iOS中评估Javascript
- 删除评估值Javascript时的错误
- Javascript-在一行中评估多个正则表达式
- 在JavaScript中评估复杂的数学表达式
- JavaScript:获取对象的评估属性
- 基本的Javascript/jQuery数学游戏:为什么可以't我第二次运行此函数时进行评估
- 使用评估函数调用 javascript 函数作为文本框 Asp.net 参数
- Webpack:是否可以在编译时评估javascript表达式
- JavaScript 评估 - 如何为对象属性赋值
- JavaScript 评估函数中的电源
- JavaScript 评估,用于在某种隔离环境中的表达式
- JavaScript:评估给定的小时是否在两个小时之间
- 使用javascript评估表单输入单选按钮
- 使用JavaScript评估URL
- JavaScript 评估无法理解字符串中的某些正则表达式
- 是Chrome Javascript评估布尔表达式正确
- Crossrider:通过javascript评估表单响应
- 如何使用JavaScript评估HTML选项
- JavaScript 评估如何