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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:58:44+00:00 2026-06-02T17:58:44+00:00

I have about 10 functions in a javascript file. Each function at the beginning

  • 0

I have about 10 functions in a javascript file. Each function at the beginning has the following statement:

var $me = $(this);

Would it be better to define $me in the global scope and then redefine it at the beginning of each function? Or does it not make any difference? for example…

var $me;
function doo() {$me = $(this)}
function foo() {$me = $(this)}
// etc. etc.
  • 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-02T17:58:46+00:00Added an answer on June 2, 2026 at 5:58 pm

    You should NEVER implement your second option with $me defined globally, but overwritten in each local function. That is a recipe for disaster. If one function calls another (or triggers an event handler), $me will get overwritten and will get trounced from the proper value in the first function. No – DO NOT DO THIS.

    The correct way is to define a NEW local variable in each function that you use it in. This is both faster and NOT prone to overwrite errors. FYI, local variables are faster to access than global variables.

    If you need a saved copy of $(this) in a function, then put:

    var $me = $(this);
    

    near the top of the function (inside the function body) so it’s a temporary local variable.

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

Sidebar

Related Questions

I have a javascript function that will show the image you're about to upload
Many people have argued about function size. They say that functions in general should
I have a simple function about neural network. This function gets a matrix, loads
I have the following JQuery function about a range slider . It's just working
I have this function handling ajax calls: function ajaxPost (divNode, parameters, file) { if
i have a javascript file about 6000+ lines of codes created by myself. Now
I have a JavaScript function (which users on this forum graciously helped me construct)
I have seen slideUp and slideDown in jQuery. What about functions/ways for sliding to
I have looked at the documentation on MSDN about these 2 functions. However, I
I have little doubt about string reading in C. string reading functions like gets,

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.