简体   繁体   English

仅在模块内使用修改后的Array.prototype

[英]Use modified Array.prototype only within module

I'm working on creating a module within a larger codebase. 我正在努力在更大的代码库中创建模块。 In several instances, I need the ability to concatenate sub arrays within larger arrays, so the array is flattened to only one dimension. 在某些情况下,我需要能够在较大的数组中串联子数组,因此该数组只能展平到一个维度。 There are a few libraries that offer similar functionality under the method name concatAll() . 在方法名称concatAll()下,有一些库提供类似的功能。

This is the function: 这是功能:

Array.prototype.concatAll = function concatAll() {
    return Array.prototype.concat.apply([], this);
};

The problem with this solution is that it modifies the Array.prototype in every instance of the entire codebase. 该解决方案的问题在于,它会修改整个代码库的每个实例中的Array.prototype I want to be able to chain this array method to other Array methods, so I can't just create a function that performs this operation. 我希望能够将此数组方法链接到其他Array方法,所以我不能只创建执行此操作的函数。

myArray
  .filter(...)
  .map(...)
  .concatAll();

I know that I can delete Array.prototype.concatAll , but our module loader is such that I only know when the module needs to be bootstrapped to the page, and not when the module is "un" bootstrapped. 我知道我可以delete Array.prototype.concatAll ,但是我们的模块加载delete Array.prototype.concatAll我只知道何时需要将模块引导到页面,而不知道何时模块被“ un”引导。 So calling delete Array.prototype.concatAll would render the method unusable within the module. 因此,调用delete Array.prototype.concatAll将使该方法在模块中不可用。

Is there a way that I can temporarily add this method the Array prototype within a module, without having every array instance inherit the method? 有没有一种方法可以在不使每个数组实例都继承该方法的情况下,将该方法临时添加到模块内的Array原型中?

The first idea is to arrange to have the code with your augmented array prototype run in a separate environment, such as an iframe or a web worker. 第一个想法是安排使代码和扩展数组原型一起在单独的环境(例如iframe或网络工作者)中运行。 The Array object in each such environment is independent, and changes to one will not affect the others. 每个这样的环境中的Array对象都是独立的,对一个对象的更改不会影响其他对象。

Alternatively, assuming your concern is that someone else not overwrite your method, you could define it as follows: 或者,假设您担心别人没有覆盖您的方法,则可以按以下方式定义它:

Object.defineProperty(Array.prototype, "concatAll", {
  get: function () {
    return function concatAll() {
      return Array.prototype.concat.apply([], this);
    };
  },
  set: function() {
    throw new Error("Attempt to overwrite Array#concatAll!");
  }
});

That would at least throw an error if someone else tries to redefine it via assignment. 如果其他人尝试通过分配重新定义它,则至少会引发错误。 Attempt to redefine it via another Object.defineProperty would also throw since the property is by default not configurable. 尝试通过另一个Object.defineProperty重新定义它,因为默认情况下该属性不可配置,因此也会抛出该Object.defineProperty

Possibly minor point: 可能是次要点:

The problem with this solution is that it modifies the Array.prototype in every instance of the entire codebase. 该解决方案的问题在于,它会修改整个代码库的每个实例中的Array.prototype。

Technically, no instances are modified, of course. 从技术上讲,当然不会修改任何实例。 If you mean that every instance will pick up the new method from the prototype, that's true, of course, but what specifically are you worried about? 如果您的意思是每个实例都将从原型中获取新方法,那当然是对的,但是您特别担心什么呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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