简体   繁体   English

如何在Node.js中使用模块等原型?

[英]How to use a prototypes such as modules in Node.js?

I want to extend standard Objects such as String with prototypes. 我想扩展标准对象,如String和原型。 For example, add diff function for an Array. 例如,为Array添加diff函数。 Good way of organization code in Node.js is modules, but how do this with prototype? Node.js中组织代码的好方法是模块,但是如何使用原型?

Example

Array.prototype.diff = function (a) {
    return this.filter(function (i) {
        return !(a.indexOf(i) > -1);
    });
};

I can not speak for nodejs, but Mozilla's JS modules are similar I guess and there I ran into the same problem once. 我不能代表nodejs,但是我猜想Mozilla的JS模块类似,我遇到过同样的问题。

Thing is that each JS module (and nodejs modules probably as well) runs in its own JavaScript context, thus the Array constructor (and prototype) from different modules are not the same. 事实上,每个JS模块(以及nodejs模块也可能)都在自己的JavaScript上下文中运行,因此来自不同模块的Array构造函数(和原型)不一样。 They are completely different objects. 它们是完全不同的对象。

I would probably have a module with a helper function that adds the according changes to a given object. 我可能有一个带有辅助函数的模块,它可以根据给定对象添加相应的更改。 This means though, that you have to call the helper function from within every module that you want to use the extended Array prototype from. 但这意味着,您必须从要使用扩展阵列原型的每个模块中调用辅助函数。

ArrayHelperModule: ArrayHelperModule:

function extendArray(arrayConstructor)
{
    arrayConstructor.prototype.diff = function (a) {
      return this.filter(function (i) {
        return !(a.indexOf(i) > -1);
      });
    };
}

exports.extendArray = extendArray;

Module that uses Array: 使用Array的模块:

var ArrayHelperModule = require("ArrayHelperModule");

// extending the Array of this module
ArrayHelperModule.extendArray(Array);

// use it
var diff = [1, 2].diff([1]);

ps hope the code is correct this way ... i just typed it in here quickly ps希望这样的代码是正确的...我只是快速输入它

This worked for me. 这对我有用。

Array.prototype.diff = function( compareArray ) {
    return this.filter(
        function( arrayItem ){
            return this.indexOf( arrayItem ) == -1;
        },
        compareArray
    );
};

You could also add a level to the prototype chain I guess but that would require more complexity and doesn't seem that useful yet. 您也可以在原型链中添加一个级别,但这需要更多的复杂性并且似乎没有用处。

Ps I'm not sure how you were planning on using the filter function. Ps我不确定你是如何计划使用过滤功能的。

var a = [1,2,3,4];
var b = [3,4,5,6];
console.log( a.diff( b ) );

// Outputs:
[ 1, 2 ]

** Due to my low rank I can not comment :( so I am writing an answer **由于我的低级别我无法发表评论:(所以我正在写一个答案

It is a very bad practice to extend the native objects, please avoid this at all costs. 扩展本机对象是一种非常糟糕的做法,请不惜一切代价避免这种情况。 Mootools has taken this approach, just google for "mootools collision" and see the issues that keep coming up.. Even though they have been moving away from it since version 1.2.4. Mootools采取了这种方法,只是谷歌“mootools碰撞”,并看到不断出现的问题..即使他们已经从1.2.4版本开始远离它。

As soon as another library is extending the native objects (mapping, graphics libs sometimes do this from past experience) you get different code expecting different behavior out of those additions. 一旦另一个库扩展本机对象(映射,图形库有时从过去的经验中做到这一点),您会得到不同的代码,期望这些添加中的行为不同。

So in short, create another "Utility" class that houses these functions and include and use that instead. 简而言之,创建另一个包含这些功能的“实用程序”类,并包含和使用它。

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

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