Function binding is the practice of taking a generic function and binding it to a specific context. For example taking a function which requires a parameter and creating a bound parameterless function where the parameter is supplied as the context.
Questions tagged [function-binding]
62 questions
51
votes
3 answers
What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?
It seems to me that, in ES6, the following two functions are very nearly identical:
function () {
return this;
}.bind(this);
() => {
return this;
};
The end result seems the same: arrow functions produce a JavaScript function object with their…
Alexis King
- 40,717
- 14
- 119
- 194
33
votes
4 answers
How to avoid bind or inline arrow functions inside render method
We should avoid method binding inside render because during re-rendering it will create the new methods instead of using the old one, that will affect the performance.
So for the scenarios like this:
Mayank Shukla
- 80,295
- 14
- 134
- 129
32
votes
2 answers
console.log() called on object other than console
I remember that always when I wanted to pass console.log as a callback parameter to some function, it didn't work unless I used the bind() method to bind console to it.
For example:
const callWithTest = callback =>…
Michał Perłakowski
- 70,955
- 24
- 137
- 155
19
votes
3 answers
JSX props should not use .bind()
How to fix this error when I have the binding this way: previously binding in constructor solved but this is bit complex for me:
{this.onClick.bind(this, 'someString')}>
and
monkeyjs
- 504
- 1
- 6
- 23
14
votes
4 answers
How does Function.bind.bind(Function.call) uncurry?
We have this line in my code base:
var uncurryThis = Function.bind.bind(Function.call);
That I'm trying to work through. Presumably, it uncurries. How do I work this out?
I guess it's a version of Function.bind whose own this is bound to…
djechlin
- 54,898
- 29
- 144
- 264
9
votes
3 answers
Can Java lambdas bind methods to their parameters?
How to pass a method as a parameter using lambdas is discussed here:
Java Pass Method as Parameter
In other languages, namely C++, it is possible to bind a function to it's parameters using Lambdas - discussed here:
Bind Vs Lambda?
Is it possible,…
bigcodeszzer
- 854
- 6
- 20
8
votes
5 answers
Correct way to bind "this" in JavaScript event callbacks?
I created a class called SearchBox to handle search interaction (delayed trigger, search on enter key press, preventing searches while one is active, synchronizing results when a search completes and the text has changed, etc.).
All the class…
Triynko
- 17,370
- 20
- 92
- 154
7
votes
0 answers
How to binding a generic function in TypeScript
I'm trying to bind a generic function to a specific context and parameter, however I can't seem to be able to figure out the correct way to do it.
Here's my code:
interface A {}
interface Repository {
save(item: T): boolean;
}
const updater…
apokryfos
- 30,388
- 6
- 55
- 83
7
votes
2 answers
Why can't I bind directly console.log on IE9 with developer tools open?
With developer tools open in IE9, this code works :
var log = Function.prototype.bind(console.log, console);
But if I type
console.log(console, console.log);
var log = console.log.bind(console);
then I get this :
Why ?
Is that a known IE bug or…
Denys Séguret
- 335,116
- 73
- 720
- 697
6
votes
2 answers
Binding and event handler — passing the event object
I have some sample code which binds an event handler as follows:
var h1=document.querySelector('h1');
h1.onclick=doit;
function doit(x) {
console.log(x);
}
When the event handler is triggered (by clicking on the h1 element), the output is…
Manngo
- 8,349
- 6
- 50
- 74
6
votes
1 answer
Explain bindbind() function
Can someone explain this function?
var bindbind = Function.prototype.bind.bind(Function.prototype.bind);
I understand the result it produce:
var bindedContextFunc = bindbind(function)(context);
bindedContextFunc(args);
But do not understand…
Nik
- 7,782
- 6
- 54
- 79
5
votes
3 answers
How to "cast" a two-argument function into a one-argument function?
In matlab, one can write:
S = @(x,y) x^2+y^2-1
G = @(x) S(x,1);
If I have a function expecting a one-argument function, I can do the above. How can I do this in c/c++?
I have a library function (from the CGAL library) that expects as an argument a…
OwenM
- 53
- 4
4
votes
3 answers
Function.prototype.bind.apply() doesn't work as expected
I've a function myfunc and want to bind it to a specific this argument and other arguments to bind as a single array, not parameters list (cause I get the parameters list as an argument of function, where this code is executed).
For that purpose I…
Boris Burkov
- 10,212
- 11
- 59
- 93
3
votes
1 answer
Generalised Curry - Javascript
While reading an article on implementation of a generalised curry in Javascript, I stumbled upon this piece of code.
function curry(fn) {
return (...xs) => {
if (xs.length === 0) {
throw Error('EMPTY INVOCATION');
}
if…
Varun
- 329
- 2
- 10
3
votes
1 answer
.bind() with event handlers
Assume I have the following simplified function.
var ex = document.getElementById('exampleElement'),
data = {
foo: 'Sample text'
};
ex.addEventListener('click', function(evt, d) {
evt.stopPropagation();
this.innerHTML =…
stafffan
- 484
- 5
- 17