You have an extra var before initializing the requestCustom property
function custFunc() {} custFunc.prototype.one = function(options) { this.requestCustom = request.defaults({ timeout: options.timeout || defaultTimeout }); }); custFunc.prototype.two = function(options) { this.requestCustom.get(/****/); });
In addition, you need to understand that before using the two method, you must always call one ;
var f = new custFunc(); f.one({}); f.two({});
Now, if request.defaults returns an object with a get method, everything should work.
Perhaps you can use the LazyLoad pattern:
custFunc.prototype.two = function(options) { if (!this.requestCustom) { this.one(options); } this.requestCustom.get(/****/); });
@PavelMayorov suggestion:
function custFunc(options) { this.options = options; } custFunc.prototype.one = function() { this.requestCustom = request.defaults({ timeout: this.options.timeout || defaultTimeout }); }); custFunc.prototype.two = function(options) { if (!this.requestCustom) { this.one(); } this.requestCustom.get(/****/); }); /// var f = new custFunc({timeout: 1e3}); f.two({id: 5});
requestcomes from. And why therequestWrapperproperty should appear - vp_arthvarbefore initializing the requestCustom property - vp_arth