Questions tagged [function-binding]

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.

66 questions
56
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
  • 43,109
  • 15
  • 131
  • 205
37
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
  • 100,735
  • 18
  • 158
  • 142
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
  • 88,409
  • 26
  • 156
  • 177
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 a bit complex for me: {this.onClick.bind(this, 'someString')}> and
monkeyjs
  • 604
  • 2
  • 7
  • 29
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
  • 59,258
  • 35
  • 162
  • 290
11
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
  • 916
  • 1
  • 8
  • 27
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
  • 18,766
  • 21
  • 107
  • 173
7
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
  • 14,066
  • 10
  • 88
  • 110
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
  • 372,613
  • 87
  • 782
  • 758
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
  • 9,063
  • 7
  • 66
  • 81
5
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
  • 13,420
  • 17
  • 74
  • 109
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
1 answer

Bind "this" to TypeScript Lambda

Say I have a lambda in TypeScript: myArray.forEach(o => o.x = this.x); The value of this becomes window instead of the calling object. What I'd really like to do is: myArray.forEach(o => { o.x = this.x; }.bind(this)); But I don't see that as an…
Josh M.
  • 26,437
  • 24
  • 119
  • 200
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…
3
votes
2 answers

Java: Function with one varargs argument and function with same name and one argument of same type is allowed?

While preparing for Java certification exam I was quite surprised to see that Java allows this: public class Consumer { public void buy(Object o) { System.out.println("Buying one object"); } public void buy(Object... o) { …
DanielBK
  • 892
  • 8
  • 23
1
2 3 4 5