0

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.

1 Answer 1

1

bind returns a new function.

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

Full example:

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

test.bind({test: 'test'})();
Sign up to request clarification or add additional context in comments.

3 Comments

Ok I see the problem when I use bind() I am losing the jquery behavior of each() passing a reference to the selector. How can I retain that? i.e. inside of connect() this is now just an object with single = true. I don't have a reference to the jQuery element any longer.
@Justin, The second argument passed to the function will be the element.
See my jsFiddle (jsfiddle.net/2VXHJ). Noticed how the console.log(this) does not include a jQuery reference to the object anymore. Without bind() $(this) is a reference to the jQuery element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.