Is there an equivalent to std :: bind in javascript or node.js?

This is a long snapshot, but I was wondering if there is such a thing as C ++ std :: bind in javascript or node.js? Here is an example where I felt the need for binding:

var writeResponse = function(response, result) { response.write(JSON.stringify(result)); response.end(); } app.get('/sites', function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); dbaccess.exec(query, function(result) { res.write(JSON.stringify(result)); res.end(); }); }); 

Instead of passing a callback to dbaccesss.exec, I would like to pass a pointer to a function that takes one parameter. In C ++, I would pass this:

 std::bind(writeResponse, res) 

This will lead to a function that takes one parameter (the β€œresult” in my case), which I could pass instead of an anonymous callback. Right now I am duplicating all this code in an anonymous function for each route in my express application.

+5
source share
5 answers

As long as it exists, I would be more inclined to do this with closure:

 function writeResponse(res) { return function(result) { res.write(JSON.stringify(result)); res.end(); }; } // and then... dbaccess.exec(query, writeResponse(res)); 
+4
source

If I understand what you are trying to do, I should point to Function.prototype.bind . It works as you described:

 app.get('/sites', function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); dbaccess.exec(query, writeResponse.bind(null, res)); }); 
+3
source

Although it is slightly different from the bind function found in STL , you can use <function>.bind , which is part of the function prototype in JavaScript.

The bind method returns the newly created function object (do not forget that function are the first citizens in JavaScript and are created starting with the function prototype), which takes N minus M parameters (in JavaScript this is a weak restriction, it will never accept as many parameters as you pass it on, but there is no guarantee that they will be used), where N is the initial number of received parameters, and M is related.

The main difference is that bind also accepts a scope object as the first argument, which will be available from the function you just created as the this link, so you can literally change and enter the this link at runtime.

You can find the bind documentation here.

As mentioned by someone, you can also rely on closure to get your target in almost all cases where you can use bind.

+3
source

Not sure if they are still supported in NodeJS, but if so, you can also easily use the arrow functions.

 app.get('/sites', function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); dbaccess.exec(query, r => writeResponse(res, r)) }); 

They also preserve the lexical meaning of this , which is nice if necessary.

This is roughly equivalent to this:

 app.get('/sites', function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); dbaccess.exec(query, function(r) { return writeResponse(res, r); }) }); 

although it has this defined by .exec() .

+1
source

It exists, there are two methods. Call and apply , which are slightly different.

There is a bind method, but it does something else (changes the value of this when the function is called).

There is no such thing as a "function pointer". I think you need currying here:

 function currier(that, fn) { var args = [].slice.call(arguments, 2); return function() { return fn.apply(that, args); } } 
-1
source

Source: https://habr.com/ru/post/1234124/


All Articles