0

Consider this example:

wofForm = function( $form ) {
 this.form = $form;
 this.select = this.form.find(".sm-product-select");
 this.productChange();
}
wofForm.prototype.productChange = function() {
 this.select.on("change", **this.checkIfVariableProduct** )
}
wofForm.prototype.checkIfVariableProduct = function() {
 console.log("searching ", this );
} 

Why when I pass the this.checkIfVariableProduct inside productChange it's actually passing the select object, instead of wofForm instance? I would like to pass the wofForm instance further, not the jQuery object of the select.

Pointy
415k62 gold badges600 silver badges633 bronze badges
asked Oct 24, 2015 at 14:05
0

1 Answer 1

3

The this value of the handler is set by jQuery's on method. "When jQuery calls a handler, the this keyword is a reference to the element where the event is being delivered." For setting the this value manually you can use the Function.prototype.bind or $.proxy method.

 this.select.on("change", this.checkIfVariableProduct.bind(this) );
answered Oct 24, 2015 at 14:08
Sign up to request clarification or add additional context in comments.

2 Comments

I think I misunderstood the question
An example using $.proxy, as mentioned, would be a nice addition to this good answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.