Bind named function to context

bind javascript w3schools
javascript bind this to callback
javascript bind event
angular function bind
javascript bind null
javascript bind vs call
typescript bind
promise bind

I have a named function like this

function handler(arg) {
  this.arg = arg;
}

Is there a way to bind context to this function?

Im looking for something like this

function handler(arg) {
  this.arg = arg;
}.bind(this);

that will allow to call this function in any context, but it will always use binded context.

For example I have another function (in vendor code, that i cant edit)

addEventListener(callback) {
   callback(currentArg);
}

where I will pass handler and it will be executed with binded context

I'm not sure where and how you are going to use it but you can use a function expression instead of function deceleration:

 var handler = function handler(arg) {
  this.arg = arg;
}.bind(this)

Or just use an arrow function which will use a lexical context for this:

var handler = (arg) => {...}

How-to: call() , apply() and bind() in JavaScript, The result of func.bind(context) is a special function-like “exotic can create an independent function with a readable name ( double , triple ). The result of func.bind(context) is a special function-like “exotic object”, that is callable as function and transparently passes the call to func setting this=context. In other words, calling boundFunc is like func with fixed this .

From MDN

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

According to this definition you can do the following

function handler(arg) {
  this.arg = arg;
}

var newFunc = handler.bind(newContext,arg);

Function.prototype.bind, How to bind this to context you have no access to using closure. Joe Chasinga Most of the time, this is bound to whatever context the function is called: // This is I was expecting this.name to refer to Foo 's instance's name . Bind context to the given function and preserves her name. Or set new name of the given function. It also handles `.toString` correctly. - tunnckoCore/bind-context Bind context to the given function and preserves her name.

If you just want to avoid context change, you can use arrow function:

handler = (arg) => {
    this.arg = arg;
};

JavaScript, You can just as easily name the variable boogabooga . Context is the object that a function, having been executed within said object, has  Introduced in Windows XP SP2. Specify this bind context to force a folder shortcut to resolve the link that points to its target. A folder shortcut is a folder item that points to another folder item in the same namespace, using a link (shortcut) to hold the IDList of the target.

Javascript call() & apply() vs bind()?, You can use call() / apply() to invoke the function immediately. bind() will have the correct context ("this") for calling the original function. //Demo with javascript .call() var obj = {name:"Niladri"}; var greeting = function(a,b,c){  Context.bind() is used to add a binding to a context. It accepts as arguments the name of the object and the object to be bound. It accepts as arguments the name of the object and the object to be bound.

Function binding, The bind() method creates a new function that, when called, has its this whose name property is not derived from the original function name. The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. The source for this interactive example is stored in a GitHub repository.

Context Binding in Javascript with Closure - Joe Chasinga, In the global context “this” refers to either window object or is undefined if the '​strict mode' is used. The bind() method creates a new function where “this” refers to the parameter in the parenthesis in the above case var name = person​.prop. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function. Binding to a function is a way of declaratively connecting another resource to the function; bindings may be connected as input bindings, output bindings, or both. Data from bindings is provided to the function as parameters.

Comments
  • looks like a classic use case for arrow functions
  • I believe if you declare handler = handler.bind(this) in another context, say another function it will bind to handlers 'this' to that context .
  • Can you give more details? Like where you want to use this and why?
  • Use addEventListener(handler.bind(this))
  • addEventListener(handler.bind(this)) is generally good advice, but if there is some other function like removeEventListener - it is impossible to perform removeEventListener(handler.bind(this))
  • Thats ok, thanks. Just for curious how do you think is it realy impossible to solve this via named functions and it could be done only by anonymous?
  • it doesn't have to be anonymous, you can (and should!) name these functions (I've updated my answer to reflect this). but it has to be a function expression (on the right side of the equal sign). with function declarations you can't reassign to that label, after all bind will return a new function instance.
  • You cannot bind an arrow function. It will always be called with the context in which it was defined but the questions says call this function in any context, but it will always use binded context.