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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:13:19+00:00 2026-05-19T09:13:19+00:00

What is the difference between including private functions in a jQuery plugin in the

  • 0

What is the difference between including private functions in a jQuery plugin in the examples below:

Outside the loop:

    (function( $ ){
      var defaults = {};

      $.fn.cmFlex = function(opts) {

        this.each(function() {
            var $this = $(this);
            //Element specific options
            var o = $.extend({}, defaults, opts);

            //Code here
        });

        function f1(){....
        function f3(){....
        function f2(){....

      };
    })( jQuery );

Inside the loop:

    (function( $ ){
      var defaults = {};

      $.fn.cmFlex = function(opts) {

        this.each(function() {
            var $this = $(this);
            //Element specific options
            var o = $.extend({}, defaults, opts);

            function f1(){....
            function f3(){....
            function f2(){....
        });     


      };
    })( jQuery );

The advantage of including the functions in the loop is that i will be able to access the $this variable as well as the Element specific options from f1() f2() f3(), are there any disadvantages to 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-05-19T09:13:19+00:00Added an answer on May 19, 2026 at 9:13 am

    Always follow the concept of giving the least privilege to all your code. If no other code needs to reference those functions, there’s no need to define them outside and changing those functions is simpler since you are guaranteed that they are not used anywhere else but within the loop.

    If f1,f2,f3 needed access to closure variables within the looping function, they’d have to be defined in the function no matter what.

    However there’s a caveat. Defining them within a function requires creating a new closure every time the loop function is run. Depending on how tight the loop is, it could affect performance. My point is: premature optimization is bad, but keep it in mind.

    FYI: Here’s another way to do it (if you don’t need the closure variables) that avoids creating the closures for every iteration of the loop. It looks a bit complex, so it’s not for the faint of heart

    (function( $ ){
      var defaults = {};
    
      $.fn.cmFlex = function(opts) {
    
        this.each((function() {
            function f1(){}
            function f3(){}
            function f2(){}
            // This is the method that will be called for each iteration of the loop. It now
            // has access to f1,f2,f3 but doesn't need to create a closure for each iteration.  
            return function() {
              var $this = $(this);
              //Element specific options
              var o = $.extend({}, defaults, opts);
              f1();
              f2();
              f3();
            };
        })());
      };
    })( jQuery );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When including a header file in C++, what's the difference between... including the .h
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
What is the difference between early and late binding?
Is there a performance difference between i++ and ++i if the resulting value is
What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass
What is the difference between TrueType fonts and Type-1 fonts?
What is the difference between Build Solution and Batch Build in Visual Studio 2008?
What is the difference between a Group and a Match in .NET's RegEx?
If one Googles for difference between notify() and notifyAll() then a lot of explanations
What is the difference between publishing a website with visual studio and just copying

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.