Rhino 1.5 R5 在查找数组差异时未正确评估 JavaScript
Rhino 1.5 R5 not evaluating JavaScript correctly when finding array differences
我在使用 Rhino 1.5 R5(Rhino 1.5 release 5 2004 03 25)的应用程序中评估某些 JavaScript 时遇到问题。我无法独立更新 Rhino。
我有两个数组:原始数据 (pastedArr) 和仅包含有效值的数组 (validatedArr),我正在尝试找出差异("bad" 值)。我在网上找到了一个函数来帮助 (sym),我不得不为每个原型制作原型,但它在 jsfiddle (https://jsfiddle.net/zr3abc0c/).
中完美运行
在应用程序中,使用 Rhino,它不会 return 整数元素(即使它们存储为字符串),如下面的脚本和 fiddle link 在 jsfiddle 上 returns ["33"] 但在我的应用程序中为 null。如果 parameter0 是 ["33","1A","ABC"] 它只会 return 在 app.
中 ["1A","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 将整数属性视为特殊的,因为它们在数组中的使用。我推测 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));
我在使用 Rhino 1.5 R5(Rhino 1.5 release 5 2004 03 25)的应用程序中评估某些 JavaScript 时遇到问题。我无法独立更新 Rhino。
我有两个数组:原始数据 (pastedArr) 和仅包含有效值的数组 (validatedArr),我正在尝试找出差异("bad" 值)。我在网上找到了一个函数来帮助 (sym),我不得不为每个原型制作原型,但它在 jsfiddle (https://jsfiddle.net/zr3abc0c/).
中完美运行在应用程序中,使用 Rhino,它不会 return 整数元素(即使它们存储为字符串),如下面的脚本和 fiddle link 在 jsfiddle 上 returns ["33"] 但在我的应用程序中为 null。如果 parameter0 是 ["33","1A","ABC"] 它只会 return 在 app.
中 ["1A","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 将整数属性视为特殊的,因为它们在数组中的使用。我推测 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));