Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why javascript bind doesn't work

function:

function talk(){ 
        console.log(this.name + " dice: ");
}

var Person = function(name, surname){
    this.name = name;
    this.surname = surname;
}

var p = new Person("Mark", "Red");

talk.bind(p);

what's wrong with bind?

like image 878
Donovant Avatar asked Apr 10 '14 15:04

Donovant


People also ask

How does bind work in JavaScript?

The bind() method creates a new function, when invoked, has the this sets to a provided value. The bind() method allows an object to borrow a method from another object without making a copy of that method. This is known as function borrowing in JavaScript.

Should you use BIND in JavaScript?

. bind() is used when you need to pass a callback (e.g. some sort of function reference), but you want the caller to call your function with a specific this value.

How do you bind variables in JavaScript?

We use the Bind() method to call a function with the this value, this keyword refers to the same object which is currently selected . In other words, bind() method allows us to easily set which object will be bound by the this keyword when a function or method is invoked.

What is the purpose of the bind () function?

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.

What is a bind function in JavaScript?

Description. The bind () function creates a new bound function, which is an exotic function object (a term from ECMAScript 2015) that wraps the original function object. Calling the bound function generally results in the execution of its wrapped function. A bound function has the following internal properties:

What is the difference between call () and bind () methods in JavaScript?

In this syntax, the bind () method returns a copy of the function fn with the specific this value ( thisArg) and arguments ( arg1, arg2, …). Unlike the call () and apply () methods, the bind () method doesn’t immediately execute the function. It just returns a new version of the function whose this sets to thisArg argument.

What happens if no arguments are provided to bind a function?

If no arguments are provided to bind , or if the thisArg is null or undefined, the this of the executing scope is treated as the thisArg for the new function. Arguments to prepend to arguments provided to the bound function when invoking func . A copy of the given function with the specified this value, and initial arguments (if provided).

Does function bind work in Internet Explorer 8?

We’ve created a new function that, when executed, has its this set to foo — not the global scope, as in the example where we called bar ();. As you can see, unfortunately, Function.prototype.bind isn’t supported in Internet Explorer 8 and below, so you’ll run into problems if you try to use it without a fallback.


1 Answers

It does work, talk.bind(p) returns the bound function:

talk.bind(p)();

like image 153
Rob M. Avatar answered Sep 17 '22 15:09

Rob M.