It looks like you are extending prototype of an Array class/type. So it should already be available everywhere that A.js
script of yours is included. No need for additional exports.
If you're using some kind of "compiler" (Rollup, Webpack, etc...) that builds single JS file from separate sources, just make sure A.js
is compiled in, and before other functions/code is executed.
However, it's not encouraged to extend basic types like that, because you never know if something else does not try to extend the same type with the same function name, but different results, which can be hard to debug later.
You can refactor it into stand-alone function instead. Something like this:
function isArrayEqual (a, b) {
// if the other array is a falsy value, return
if (!a || !b) return false
// if any of them is not an Array, return
if (!Array.isArray(a) || !Array.isArray(b)) return false
// compare lengths - can save a lot of time
if (a.length != b.length) return false
for (var i = 0, l = a.length; i < l; i++) {
// Check if we have nested arrays
if (a[i] instanceof Array || b[i] instanceof Array) {
// recurse into the nested arrays
if (!isArrayEqual(a[i], b[i])) return false
} else if (a[i] != b[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false
}
}
return true
}
console.log('[] vs []', isArrayEqual([], [])); // true
console.log('[1] vs []', isArrayEqual([1], [])); // false
console.log('[] vs [1]', isArrayEqual([], [1])); // false
console.log('[1] vs [1]', isArrayEqual([1], [1])); // true
console.log('[1] vs [2]', isArrayEqual([1], [2])); // false
console.log('[[1], [2]] vs [[1], [2]]', isArrayEqual([[1], [2]], [[1], [2]])); // true
console.log('[[1], [2]] vs [[1], []]', isArrayEqual([[1], [2]], [[1], []])); // false
console.log('[1, [2]] vs [1, 2]', isArrayEqual([1, [2]], [1, 2])); // false
// Code does not strict-equal so following will return true:
console.log('[1] vs ["1"]', isArrayEqual([1], ['1'])); // true!