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

The Archive Base Latest Questions

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

For a while now, I’m been using jQuery to offer enhancements via JavaScript on

  • 0

For a while now, I’m been using jQuery to offer enhancements via JavaScript on my websites. However, I’m now beginning to ponder—from a performance point of view—what is the best way to organise my file of functions?

Up until now, I’ve done something similar to the following:

$(document).ready(function() {
    foo();
    bar();
});
function foo() {
    // do something here
};
function bar() {
    // do something else here, and so on...
};

However, I’ve since seen JavaScript file layouts like this:

$(document).ready(function() {
    $(selector).foo();
    $(selector).bar();
});
$.fn.foo = function() {
    $(this).each(function() {
        // do something here and return
    });
};
$.fn.bar = function() {
    $(this).each(function() {
        // do something here and return
    });
};

Is it better practice to extend jQuery and assign methods to a selector as in the second example? Or is it still OK to define individual functions as the first example?

  • 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-15T23:39:30+00:00Added an answer on May 15, 2026 at 11:39 pm

    It very much depends (the catch-all answer!). If a piece of functionality makes sense as a plugin i.e. can be reused in different ways on your site, then make it a plugin. Similarly, if a function is useful all over the place e.g. a date parsing function, make it a utility function.

    There are a couple of points that I think are really worth bearing in mind – scope and pollution

    scope
    If a function needs to be in a scope that makes it accessible to functions in other scopes, then make it so. If it doesn’t, don’t. Which leads onto

    pollution
    try not to pollute the global namespace/object with numerous different functions. Organise your code into related functions by utilising object literals such that only one property is created on the global object that contains objects with properties containing related functions. For example,

    var date = {
        parse : function(s) {
            // some date parsing function
        },
        format : function(date, patten) {
            // some date formatting function
        }
    }
    

    In summary,

    • attach to the jQuery object when it make sense to (reusable plugin, common utility function)
    • declare functions in the narrowest scope that provides the accessibility to them that is required (use closures where necessary).
    • use object literals to group related functions and prevent pollution
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are looking for Simple Entities Closest quick start i… May 16, 2026 at 6:32 pm
  • Editorial Team
    Editorial Team added an answer I have a very different solution for you. Write a… May 16, 2026 at 6:32 pm
  • Editorial Team
    Editorial Team added an answer <fx:Script> <![CDATA[ private var _label:String; public function get label() :… May 16, 2026 at 6:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For while now I have been using ezpublish as a framework, and CMS when
For a while now, I have been using UltraEdit on my Windows box. The
For a while now I've been using subversion for my personal projects. More and
I've been writing PHP applications using PHP for a while in WAMP. Now I'm
It's been a while now since Microsoft stopped supporting Embedded Visual C++ 4.0 (eVC
For a little while now I've been searching for a code to get URL's
Been investigating for a while now and keep hitting a brick wall. I am
For a while now we've been having anecdotal slowness on our newly-minted (VMWare-based) SQL
I've been programming a while now at school, and I'm working on my first
I've been looking this over for a while now and can't seem to pinpoint

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.