最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Where to change objects prototypes in node.js? - Stack Overflow

programmeradmin1浏览0评论

I want to add or override some standard methods of Object, Function and Array (e.g., like suggested in this answer) in node.js application. How should I do all the "patches" in just one module so that it affects all my other modules?

Will it be enough if I do it in a module that is just require'd or it won't work because the two modules have different global namespaces so they have different Object?... Or should I run some initialisation function after require that makes all these "patches" working in this module too?

I want to add or override some standard methods of Object, Function and Array (e.g., like suggested in this answer) in node.js application. How should I do all the "patches" in just one module so that it affects all my other modules?

Will it be enough if I do it in a module that is just require'd or it won't work because the two modules have different global namespaces so they have different Object?... Or should I run some initialisation function after require that makes all these "patches" working in this module too?

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Jan 15, 2013 at 0:21 espesp 7,6876 gold badges56 silver badges84 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14
//require the util.js file 
require('./util.js');

var a = [];
a.doSomething();

in your "util.js" file:

//in your util.js file you don't have to write a module, just write your code...
Array.prototype.doSomething = function(){console.log("doSomething")};

Each file loaded shares the same primordial objects like Object, Array, etc, unless run in a different vm Context, so requiring the file once in your initialization will make the changes everywhere.

发布评论

评论列表(0)

  1. 暂无评论