Justin
Justin

Reputation: 45350

Binding a value into an exsiting function in JavaScript

I am trying to pass an argument single into a preexisting function in javascript with bind().

var connect = function(index) {
    console.log(this.single);
    ...
}

var connect_fn = connect;
connect_fn.bind({
    single: true
});

$(".box").each(connect_fn);

Unfortunately, in the console.log() this.single is undefined.

Upvotes: 0

Views: 32

Answers (1)

plalx
plalx

Reputation: 43718

bind returns a new function.

connect_fn = connect_fn.bind({
    single: true
});

Full example:

function test() {
    console.log(this.test);
}

test.bind({test: 'test'})();

Upvotes: 1

Related Questions