简体   繁体   中英

how to export custom array.prototype method from js file in react

I have method for arrays to check if they are equal. So i want to export this method for having ability to use this method in all project. I have file A.js. How to export this method from there?

Array.prototype.equals = function(array) {
  // if the other array is a falsy value, return
  if (!array) return false

  // compare lengths - can save a lot of time
  if (this.length != array.length) return false

  for (var i = 0, l = this.length; i < l; i++) {
    // Check if we have nested arrays
    if (this[i] instanceof Array && array[i] instanceof Array) {
      // recurse into the nested arrays
      if (!this[i].equals(array[i])) return false
    } else if (this[i] != array[i]) {
      // Warning - two different object instances will never be equal: {x:20} != {x:20}
      return false
    }
  }
  return true
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, 'equals', { enumerable: false })

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM