Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:58:46+00:00 2026-06-15T10:58:46+00:00

I have the following code: bindEvents: function() { $(‘#weight’).click($.proxy(function(){ this.changeWeight($(‘#weight’).is(‘:checked’)); },this)); $(‘#produce’).change($.proxy(function(){ this.changeProduce(); },this));

  • 0

I have the following code:

bindEvents: function() {
    $('#weight').click($.proxy(function(){
        this.changeWeight($('#weight').is(':checked'));
    },this));
    $('#produce').change($.proxy(function(){
        this.changeProduce();
    },this));
    $('.help-gtin').click($.proxy(function(){
        if ($('#help-gtin').is(':hidden')) {
            $('#help-gtin').slideDown();
        } else {
            $('#help-gtin').slideUp();
        }
        this.refreshGtin();
    },this);

    $('[name="order_production"]').click($.proxy(function(){
        this.changeProduction();
    },this));

},

How can I reduce this repetive code $.proxy call as all methods inside bindEvents will be need call in this scope?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:58:47+00:00Added an answer on June 15, 2026 at 10:58 am

    Take advantage of the fact that they’re already closures, by setting a variable equal to this and then using that:

    bindEvents: function() {
        var self = this; // <==== Set the variable
    
        $('#weight').click(function(){
            // v--- Use it (throughout)
            self.changeWeight($('#weight').is(':checked'));
        });
        $('#produce').change(function(){
            self.changeProduce();
        });
        $('.help-gtin').click(function(){
            if ($('#help-gtin').is(':hidden')) {
                $('#help-gtin').slideDown();
            } else {
                $('#help-gtin').slideUp();
            }
            self.refreshGtin();
        });
    
        $('[name="order_production"]').click(function(){
            self.changeProduction();
        });
    
    },
    

    All of the functions you define within bindEvents “close over” the context of the call to bindEvents, and have access to the local variables associated with that call. Unlike this, those variables don’t change depending on how the functions are called.

    This also has the advantage that you can use this within the event handlers with its jQuery meaning, the element on which you hooked the event (this saves your looking it up again, for instance within your click handler on #weight).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code function Model(onChanged) { this.array = new Array(); this.onChanged = onChanged;
Have following code. object.waypoint=function () { this.uw=setInterval( function() { console.log(this); }, 200); } How
I have following code: string date = 13.04.2012; string date2 = (DateTime.Parse(date).AddDays(1)).ToString(); This is
I have following code $("#in").datepicker({ minDate: 0, onSelect: function (dateText, inst) { dateText =
I have following code in a html form <select name=category class=input onchange=ShowTB(this,'suggest');> <option value=0
I have following code in php: $mode = $this->input->get('mode'); // may return string or
I have following code #file 1 a.pm package a; @Export=(test); print test; #hashing this
I have following code: $fh=fopen($api,r); $theData = fgets($fh); echo $theData; This throws the error
I have following code (modified code from this tutorial ): -(NSMutableArray *) getChampionDatabase {
I have following code in VS2008 If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then RoleCheckBox.Checked = True

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.