Second console.log() executed by WSFunctions[this.name](); will print undentified. I was wondering if I am able somehow to inherit DoThisAndThat in my Call() function. I didn’t want to pass params in the way WSFunctions[this.name](this.params) as while project grows there may be more than this.params to pass.
function WS(name, params) {
this.name = name;
this.params = params;
}
WS.prototype.Call = function() {
if (typeof WSFunctions[this.name] !== "function") {
return false;
}
console.log(this.params);
WSFunctions[this.name]();
return true;
}
var WSFunctions = {
'ScreenRightGuest': function() {
// .. whatever ..
return true;
},
'DoThisAndThat': function() {
console.log(this.params);
return true;
}
}
new WS('DoThisAndThat', { login: '123', pass: 'abc' }).Call();
Thanks in advance
Mike
You can explicitly set what
thisshould refer to in a function using.call[MDN] or.apply[MDN]This will call
WSFunctions[this.name]withthisbeing set to whatthisrefers to in the caller (in this case the instance created bynew WS(...)).Also have a look at this page which thoroughly explains how
thisworks.