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]
66 questions
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
- 482
- 6
- 17
2
votes
1 answer
Why does bind fix "failed to execute 'fetch' on 'Window' illegal invocation" error?
I'm just curious,
This...
let s = { f: window.fetch };
s.f("https://www.google.com");
fails with
Failed to execute 'fetch' on 'Window': Illegal invocation
While this works...
let s = { f: window.fetch.bind(window) };…

jeffbRTC
- 1,941
- 10
- 29
2
votes
1 answer
TypeError: Class method is not a function, confused about binding this
I have this class Game.
class Game {
constructor(player) {
this.player1 = player;
}
getHost() {
// player has property nickname
return `Host: ${this.player1.nickname}`;
}
}
I believe the…

wf858
- 23
- 2
2
votes
1 answer
Using pointers to member to pass member function as arguments
#include
#include
class Foo
{
public:
Foo(int value)
: value_(value){}
void print()
{
std::cout << "member print: " << value_ << std::endl;
}
int value_;
};
void print()
{
std::cout…

Vince
- 3,979
- 10
- 41
- 69
2
votes
2 answers
XMLHttpRequest and Binding a Callback not working
I have a simple get function using XMLHttpRequest which accepts a callback parameter. The plan is to invoke the callback on the onload event handler.
Here is a simplified version:
get(url,doit);
function doit(data) {
alert(data)
}
function…

Manngo
- 14,066
- 10
- 88
- 110
2
votes
1 answer
Javascript: Why am I able to access Function.prototype.call without mentioning the prototype?
Here is a snippet to explain my question:
+function(str) {
return str.replace(/^[a-z]|\s[a-z]/g,
Function.call.bind(String.prototype.toUpperCase));
}('foo bar baz.'); //Returns Foo Bar Baz.
Function.call works, but String.toUpperCase…

s4san
- 319
- 2
- 9
2
votes
1 answer
ESLint and This-Bind operator
The This-Bind operator is a proposal for convenient this method-binding syntax for ES7:
// this-bind via '::'
$(".some-link").on("click", ::view.reset);
// oldschool .bind(this, ...)
$(".some-link").on("click", view.reset.bind(view))
// or even…

ankhzet
- 2,517
- 1
- 24
- 31
2
votes
1 answer
How to curry jQuery methods -- which `this` value will pass the jQuery object?
If I want to create a jQuery function using Function.prototype.bind, i.e. to the need for a wrapping function, which this value do I supply? The following trivial example does not seem to work:
// e.g.: $.fn.outerHeight() with argument true gets…

lunelson
- 561
- 6
- 11
2
votes
0 answers
How to get function binders into one class?
I'm trying to understand how function binders work. So far I have got a small binder for 0-2 Arguments which works fine, but I don't know how to get it into one class (such as the function<>), to store it in a collection.
template
struct…

Gonger96
- 21
- 2
2
votes
0 answers
Custom function to construct an object in an object pool implementation?
I'm writing a custom object pool because I couldn't find any that meet the specific requirements of my application. The object pool itself follows a simple model of "fetch a pre-allocated object from list if exists, otherwise create a new one and…

jwalk
- 1,120
- 11
- 27
1
vote
1 answer
Bind function arguments in Julia
Does Julia provide something similar to std::bind in C++? I wish to do something along the lines of:
function add(x, y)
return x + y
end
add45 = bind(add, 4, 5)
add2 = bind(add, _1, 2)
add3 = bind(add, 3, _2)
And if this is possible does it…

operator
- 155
- 2
- 7
1
vote
1 answer
Function-binding with super keyword in javascript
I would like to call "super" from a bound function.
Here is my use case: I have many child classes from different parents. I'd like to bind the same function to all of them (instead of copy pasting it). That function needs to call the "super"…

FourchetteVerte
- 333
- 3
- 7
1
vote
1 answer
Is JavaScript function.bind() supported in google sheets?
Here a simple code i'm trying to run in google sheets script.
The purpose is supplying the foreach callback function additional parameters.
function print(str, num) {
Logger.log(str + ": " + num);
}
function test()
{
var array = [1,2,3];
…

OJNSim
- 736
- 1
- 6
- 22
1
vote
1 answer
Azure function (Queue trigger), web app shared data type best practice
An Azure function has a Queue trigger, and the data coming from the Queue is serialized Json...i.e., the web app serializes object of type DataType1 and writes it to the Queue. The Azure function has to be able to resolve the namespace for…

Mike
- 1,010
- 1
- 14
- 33
1
vote
2 answers
Enforce typings when using bind
I'm trying to create a bound version of a function which has its arguments pre-set however I am unable to get any sort of type checks on the bind method.
Here's my code:
interface I {
a: string;
b: string;
}
function doSomethingWithValue(value:…

apokryfos
- 38,771
- 9
- 70
- 114