esp*_*esp 5 javascript prototype module node.js
我想在node.js应用程序中添加或覆盖Object,Function和Array的一些标准方法(例如,如本答案中建议的那样).我应该如何在一个模块中执行所有"补丁",以便影响我的所有其他模块?
这将是不够的,如果我在一个模块中做到这一点,这只是require"d,否则将无法工作,因为两个模块有不同的全局名称空间,使他们有不同的对象?......或者我应该运行一些初始化功能之后require,使所有这些"补丁"也在这个模块中工作?
use*_*801 11
//require the util.js file
require('./util.js');
var a = [];
a.doSomething();
Run Code Online (Sandbox Code Playgroud)
在"util.js"文件中:
//in your util.js file you don't have to write a module, just write your code...
Array.prototype.doSomething = function(){console.log("doSomething")};
Run Code Online (Sandbox Code Playgroud)
加载的每个文件共享相同的原始对象,如Object,Array等,除非在不同的vm上下文中运行,因此在初始化时要求文件一次将在各处进行更改.
| 归档时间: |
|
| 查看次数: |
3864 次 |
| 最近记录: |