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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:15:39+00:00 2026-05-29T19:15:39+00:00

I’ve written my first jQuery plugin and it goes a little something like this.

  • 0

I’ve written my first jQuery plugin and it goes a little something like this.

(function ($) {
    $.fn.MyFancyPlugin= function (settings) { 

        var defaults = {
            someSetting = 'default for some setting'
        };

        var settings = $.extend({}, defaults, settings);

        function foo(){
            // do stuff
        };

        function bar(){
            // do stuff
        };

        return this.each(function (){
            // do more stuff
        });
    };
})(jQuery);

The plugin works just fine, but I’m so freaking anal about the looks that I hate the way the functions are nested.

My “preference” for the look of my JS is something like this.

MyNamespace.Class1 = {

    foo: 'foo',

    bar: function() {
        return 'bar';
    }
};

But I can’t seem to wrap my head around this when making a jQuery plugin. Can someone lend a hand? I’m most concerned about the var‘s and the fact that they’re private.

On a side note, I’d like to know what the “official” names are for each of these approaches to structuring JavaScript.

  • 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-29T19:15:40+00:00Added an answer on May 29, 2026 at 7:15 pm

    There’s nothing at all wrong with your first example, which is loosely the “module” pattern, except it may be duplicating the functions unnecessarily. Your latter example is just how you assign anonymous functions to properties on an object, which is inappropriate if the functions in question don’t need to be properties of the object.

    Limiting the scope of functions by wrapping them in other functions is a fundamental JavaScript idiom, not ugly at all.

    However, if foo and bar don’t need access to the variables and arguments in scope when your plug-in function is called (and if defaults is really static), you can move them out a level so they’re not recreated every time your plug-in is called:

    (function ($) {
        var defaults = {
            someSetting: 'default for some setting'
        };
    
        function foo(){
            // do stuff
        };
    
        function bar(){
            // do stuff
        };
    
        $.fn.MyFancyPlugin= function (settings) { 
    
            var settings = $.extend({}, defaults, settings);
    
            return this.each(function (){
                // do more stuff
            });
        };
    })(jQuery);
    

    Also note that I corrected the syntax of your defaults initializer (:, not =).


    Side note: Since your plug-in function isn’t a constructor function, the overwhelming convention in the JavaScript world is to use camelCasing with an initial lower case letter (e.g., myFancyPlugin rather than MyFancyPlugin).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build

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.