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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:06:04+00:00 2026-06-10T18:06:04+00:00

I’m writing a jQuery plugin and getting regularly confused by the scope of certain

  • 0

I’m writing a jQuery plugin and getting regularly confused by the scope of certain functions (as is tradition when using jS).

A short example should help:

(function ( $ ) {

    var methods = {

        init: function ( options ) {
            var settings = $.extend ({
                a: 100,
                b: 200
            }, options);

            return this.each(function(){

                var $this = $(this);
                var return_value = $this.plugintest("method_1", settings);
                $this.plugintest("method_2", settings, return_value);
            });
        },

        method_1 : function ( settings ) {

            var method_1_value_1 = settings.a * 10,
                method_1_value_2 = settings.a * 20;

            return method_1_value_1;
        },

        method_2 : function ( settings, old_return_value ) {

            // WHAT IF I WANT BOTH method_1_value_1 AND method_1_value_2 in here?
        }
    };

    $.fn.plugintest = function (method) {

        if ( methods[method] ) {

            return methods[method].apply( this, Array.prototype.slice.call ( arguments, 1 ) );

        } else if ( typeof method === 'object' || ! method ) {

            return methods.init.apply( this, arguments );

        } else {

            $.error( 'Method ' + method + ' does not exist in jQuery.robottest' );
        }
    };
}) (jQuery);

See method_2. I want to access the values that I created in method_1, however I can only return 1 value – should I create a Global variable of some sort? What is the ‘best’ way to do this?

  • 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-10T18:06:06+00:00Added an answer on June 10, 2026 at 6:06 pm

    Variables are visible in the function where they are declared (i.e. the function where their var statement appears) and in any functions that are declared within that function.

    Here’s an example:

    (function () {
        var foo;
    
        // foo is visible here, bar is not
        // declare variables that should be visible to your whole plugin here
    
        var methods = {
            a: function () {
                var bar;
                // foo and bar are both visible here
            },
    
            b: function () {
                // foo is visible here, bar is not
            }
        };
    }());
    
    // neither foo nor bar are visible here
    

    You should never use global variables (i.e. variables that aren’t declared with the var statement inside a function). These are visible to all other code in your document. However, as long as you enclose everything in a function and always use var you’re safe.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.