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 7051109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:13:28+00:00 2026-05-28T03:13:28+00:00

Given the example below (click content on the right if you’re using the jsfiddle

  • 0

Given the example below (click “content” on the right if you’re using the jsfiddle link), why does self fall out of scope when it reaches the clickHelper()?

I can think of two ways to fix it… (1) I can pass it in as an argument, or (2) I can define var self; inside of the plugin (above function clickHelper()).

I don’t want to do #1 though, because in my real implementation, I have a bunch of arguments already and a bunch of helper functions… I was hoping to use scope to avoid passing self around everywhere.

And I don’t want to use #2 because it seems like if this plugin was used for multiple elements on the page, there could be race conditions… my assumption is that jQuery has 1 instance of the plugin, and therefore var self would only exist once.

I’m basically looking for the best practice of keeping this in scope inside of click event raised for a jQuery plugin.

http://jsfiddle.net/QZc9v/

<div id="a1">
    content
</div>

<script type="text/javascript">=
    function info(n, s) {
        var id = (typeof (s.id) == "undefined") ? '' : ': ' + s.id;
        alert(n + ': ' + s + ': ' + typeof (s) + id);
    }

    (function ($) {    
        $.fn.myPlugin = function () {
        function clickHelper() {
            info('self in the Helper...', self);
            self.innerHTML += '2';
        }
        function clickHandler() {
            var self = this;
            info('self in the Handler', self);
            self.innerHTML += '1';
            clickHelper();
        }
        this.bind('click', clickHandler);
        };
    }(jQuery));

    $(function () {
        $('#a1').myPlugin();
    });
</script>

ETA: #1 is probably the best bet if you don’t mind the extra arguments, because #2 does race condition/thread safe issues as demonstrated here (click content 1, ok, and then content 2 really quick). You can get away with #2 by defining your helper methods inside your handler method as demonstrated here, which might work well for your if all your helper methods do is support your handler method. And #3, I suppose, instead of calling JavaScript functions like you normally would clickHelper(), just use a proper closure function(this) { var self = this; clickHelper() };.

  • 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-05-28T03:13:29+00:00Added an answer on May 28, 2026 at 3:13 am

    Guide on closures and scoping

    Variables are only available to the functions in which they are created. So by creating self inside of clickHelper you are making it a local variable only to the clickHelpers function.

    By moving it into myPlugins scope its now accessible to the nested functions.

    Live Demo

     $.fn.myPlugin = function () {
            var self = {}; // self declared here instead can be used within this scope now
            function clickHelper() {
                info('self in the Helper...', self);
                self.innerHTML += '2';
            }
            function clickHandler() {
                self = this;
                info('self in the Handler', self);
                self.innerHTML += '1';
                clickHelper();
            }
            this.bind('click', clickHandler);
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following HTML structure: <div class=wrapper> <div class=top> <a href=http://example.com class=link>click here</a> </div>
Given the example below, can someone please show me how this could be called?
Given the example source code below, is it possible for someone to see the
Given the simplified example below, how would I access my custom current_status property within
Below is the code I have written based on the example given here Google
I know how to do this... I'll give example code below. But I can't
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they
Given this example: <img class=a /> <img /> <img class=a /> <img class=a id=active
Given an example of calling two web services methods from a session bean, what
Given this example XML file: <doc> <tag> Hello ! </tag> <tag> My name is

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.