简体   繁体   English

我可以限制Javascript的Array.prototype扩展的范围吗?

[英]Can I limit the scope of Javascript's Array.prototype extensions?

I'd like to limit the scope of extending Array 我想限制扩展数组的范围

Array.prototype.myExtension = function() { 
// do something;
// return something;
};

to the current JS module. 到当前的JS模块。 This extension should be invisible in other modules. 此扩展名在其他模块中应该是不可见的。

The workarounds are to sandbox an Array in another scope (an iframe or web worker) or to define the method generically and call it (or apply ) with an array object. 解决方法是在另一个作用域(iframe或Web worker)中沙箱化Array或一般地定义该方法并与一个array对象一起调用 (或应用 )。

modulenamespace.myExtension = function() { 
// do something;
// return something;
}


modulenamespace.myExtension.call(array)

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

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