I need to add some prototype to Array class,in native javascript I can do following
var myArray = Array;
myArray.prototype.myMethod = function(){}
var testArray = new myArray();
testArray.contains();
but now I need to do this by node js module and exports myArray as class so can make some object from it,how can I do this?
If you add to the Array prototype directly from within a module, it'll be accessible to the Array in the main scope
To see this, put the following line in foo.js:
Array.prototype.foo = "bar";
Then fire up the repl and run
$ node
> Array.prototype.foo
undefined // <-- Array normally doesn't have foo
> require('./foo')
{}
> Array.prototype.foo
'bar' // <-- note how it's defined now
> [].foo
'bar' // <-- as expected
You can do the same thing with other basic objects like Number