Skip to main content
added 120 characters in body
Source Link
Jorge Silva
  • 4.6k
  • 1
  • 24
  • 43

You canThe first thing you need to do thisis remove the parentheses from the function. What your code is currently expecting is for getCarModel to return a couple of waysfunction which is then being called when the event is triggered.

The most simple, understandlbe wayIt looks like this is what you would like to use the that variabledo:

var that; 
 $('#' + this.ID + ' .selMake').on('change', that.getCarModel);

Not this:

 //$('#' This+ willthis.ID execute+ the' function.selMake').on('change', thisthat.getcarModelgetCarModel());

You can also useIf you want to call the bind method in browsersfunction that support ES5.way, you can do as follows:

 var that = this;
 $('#' + this.ID + ' .selMake').on('change', thisfunction () {
     that.getCarModel.bind(this);
 });

Above, you are passing an anonymous function as an argument which will execute the code inside of it.

In the functions above, the definition of this will depend on the element that triggered the event. If you're using underscore you might want the definition of this to use itsbe tied to your _.bindthis function in orderobject, you can do the following:

The most simple, understandable way is to make it cross-browser compatibleuse the that variable:

var that; 
$('#' + this.ID + ' .selMake').on('change', _that.bindgetCarModel); // This will execute the function this.getcarModel

You can also use the bind method in browsers that support ES5.

$('#' + this.ID + ' .selMake').on('change', this.getCarModel. bind(this));

You can do this a couple of ways.

The most simple, understandlbe way is to use the that variable:

var that; 
 $('#' + this.ID + ' .selMake').on('change', that.getCarModel); // This will execute the function this.getcarModel

You can also use the bind method in browsers that support ES5.

$('#' + this.ID + ' .selMake').on('change', this.getCarModel.bind(this));

If you're using underscore you might want to use its _.bind function in order to make it cross-browser compatible:

$('#' + this.ID + ' .selMake').on('change', _.bind(this.getCarModel. this));

The first thing you need to do is remove the parentheses from the function. What your code is currently expecting is for getCarModel to return a function which is then being called when the event is triggered.

It looks like this is what you would like to do:

 $('#' + this.ID + ' .selMake').on('change', that.getCarModel);

Not this:

 $('#' + this.ID + ' .selMake').on('change', that.getCarModel());

If you want to call the function that way, you can do as follows:

 var that = this;
 $('#' + this.ID + ' .selMake').on('change', function () {
     that.getCarModel();
 });

Above, you are passing an anonymous function as an argument which will execute the code inside of it.

In the functions above, the definition of this will depend on the element that triggered the event. If you want the definition of this to be tied to your this object, you can do the following:

The most simple, understandable way is to use the that variable:

var that; 
$('#' + this.ID + ' .selMake').on('change', that.getCarModel); // This will execute the function this.getcarModel

You can also use the bind method in browsers that support ES5.

$('#' + this.ID + ' .selMake').on('change', this.getCarModel.bind(this));
Source Link
Jorge Silva
  • 4.6k
  • 1
  • 24
  • 43

You can do this a couple of ways.

The most simple, understandlbe way is to use the that variable:

var that; 
$('#' + this.ID + ' .selMake').on('change', that.getCarModel); // This will execute the function this.getcarModel

You can also use the bind method in browsers that support ES5.

$('#' + this.ID + ' .selMake').on('change', this.getCarModel.bind(this));

If you're using underscore you might want to use its _.bind function in order to make it cross-browser compatible:

$('#' + this.ID + ' .selMake').on('change', _.bind(this.getCarModel. this));