Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access object.prototype methods from another file using module.exports?

If I want to use an object and its methods in another file, how would I set up my module.exports?

Object:

var Object = function ()
{ 
...
}

Object.prototype.foo = function (param)
{
...
}

Module.export:

module.exports = {
    Object : Object
}

or

module.exports = {
    Object : Object,
    foo : Object.prototype.foo
}

?

like image 468
SweetShorts Avatar asked Oct 26 '25 06:10

SweetShorts


1 Answers

A few ways of doing this but if you're trying to access prototype methods from your other file, then you'll need to instantiate your constructor, something like:

For ex:

// lib.js

var YourThing = function () {
}

YourThing.prototype.someMethod = function () {
  console.log('do something cool');
}

module.exports = YourThing;

// index.js

var YT = require('./lib.js');
var yourThing = new YT();
yourThing.someMethod(); 
like image 50
Ignacio Avatar answered Oct 28 '25 22:10

Ignacio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!