.png without changing the volume

I want to use the JavaScript.apply method for the lean functions compiled for Node.js. The sarge.js file has the following code:

... var NimbusClient = exports.Client = function(output, pClass) { this.output = output; this.pClass = pClass; this.seqid = 0; this._reqs = {}; }; NimbusClient.prototype = {}; NimbusClient.prototype.getClusterInfo = function(callback) { this.seqid += 1; // line where error is thrown [0] this._reqs[this.seqid] = callback; this.send_getClusterInfo(); }; ... 

My server file is as follows:

 var thrift = require('thrift') , nimbus = require('./Nimbus') , connection = thrift.createConnection('127.0.0.1', 6627) , client = thrift.createClient(nimbus, connection) , ... // server initiation etc app.get('/nimbus/:command', function(req, res, next) { client[req.params.command](console.log); // direct call [1] client[req.params.command].apply(this, [console.log]); // apply call [2] }); ... 

A direct call [1] returns the values ​​as expected, but a call to Apply [2] always causes the following error on line [0]:

 TypeError: Cannot set property 'NaN' of undefined 

I tried several other scope parameters in [2]: null , nimbus , nimbus.Client , nimbus.Client.prototype , nimbus.Client.prototype[req.params.command] and client[req.params.command] , all without success.

How can I call the apply method without changing the actual scope of the called function, so that it behaves exactly as if it were called directly?

+6
source share
1 answer

Pointing the apply function to the same function as in this case, you should save the original area.

 client[req.params.command].apply(client, [console.log]); 
+11
source

All Articles