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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:30:51+00:00 2026-06-09T21:30:51+00:00

Im porting a prototype plugin to jQuery. The plugin uses the proscribed method of

  • 0

Im porting a prototype plugin to jQuery.

The plugin uses the proscribed method of collecting all the plugins methods in an object literal and then calling them like [object].[method]

What I dont understand is, in any of those methods there is use of properties (defined at the begging of the script i.e. var x = 0, var y = 0, etc) that appear to be global and not passed as arguments or properties of a specific method.

How would I do this in jQuery and is it possible?

Please refer to ‘var1’ in the code below. Where would this be set so that all methods have access to it?

Example:

;(function($){

    var methods = {
        init : function(options) {

            var config = {
            // default options...
            }

            // overide config
            var settings = $.extend(config, options);

            return this.each(function() {
                        // init code goes here...
            });
        },

        function1 : function() {
            function2();
        },

        function2 : function() {
                $(selector).css({
                  width : var1,
                });             
        },
    }

    $.fn.[PLUGINNAME] = 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 on jQuery.tooltip' );
      } 

    };

})(jQuery);
  • 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-09T21:30:53+00:00Added an answer on June 9, 2026 at 9:30 pm

    You need to declare the variable inside the self-evoking function, but outside any other function.

    (function($){
        // This variable will be available to all methods, 
        // but not outside the plugin
        var var1,
            methods = {
                init : function(options) {
                    ...
                }
                ...
            };
    })(jQuery);
    

    You could then use the init method to set the proper value to it for instance, if it is part of the initialization process, and not just a static variable.

    Since JavaScript uses functions to declare variable scope, the outer self-evoking function will make sure that the variable don’t “leak” up to the global scope, but since it is declare outside any of the inner functions, it will be available to all functions within your plugin.

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

Sidebar

Related Questions

I'm porting some WPF code to WinRT. The code uses System.Windows.Media.Animation.ParallelTimeline to syncrhonize two
I am porting visual c++ 6 application to python using ctypes. It uses same
I'm porting a Bash script to Python. The script sets LC_ALL=C and uses the
All right, so previously I asked... SOAP Prototype AJAX SOAPAction Header Question (can't hyperlink
Im porting a plugin for Wordpress from a script, and Im finding it very
Porting an application from C# (1.1 framework) to VB.NET (3.5 framework), and I have
Porting code from 32bit to 64bit. Lots of places with int len = strlen(pstr);
We're porting a legacy Java app to JRuby and would like to reuse some
I'm porting an existing app to rails that makes extensive use of stylized anchors
I'm porting the T6963-based LCD driver from AVR-GCC to the microchip C18 compiler. I

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.