lukassz lukassz - 7 months ago 21
Javascript Question

NodeJS create object of function in module

I have file item.js

exports.itemParam = function(name, price, url, id){
this.name = name;
this.price = price;
this.id = id;
this.url = url;
};


and in my www.js I require item.js

var item = require('../modules/item');


Which way can I create object of this function?

item.itemParam(a,b,c,k); // item.new itemParam(a,b,c,k); ?


When
itemParam
I have in www.js I execute this in

new itemParam(a,b,c,k);

Answer

Assuming you gonna keep your class definition, then this will work:

new item.itemParam(a,b,c,k);

itemParam function can't be used in www.js file without referring to the obejct that has one which is item.

Comments