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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:14:47+00:00 2026-05-17T20:14:47+00:00

I am really confused as to why sometimes vars will not work when var

  • 0

I am really confused as to why sometimes vars will not work when “var” is declared in front of them when used in a namespaced object. Isn’t adding “var” in front of every variable the correct thing to do to keep it outside the global namespace?

Or would creating any var without declaring “var” first in my namespaced object, ensure of this, so I don’t eed to worry about “var”?

Here’s an example of my code:

MYNAME.DoStuff = {
   initialize: function() {
       var var1 = 'name'; //1
       var2 = 'name'; //2
       this.var3 = 'name'; //3

       var $var4 = $('#' + name); //4
       $var5 = $('#' + name); //5
       this.$var6 = $('#' + name); //6
   },

   linkStuff: function() {
       // then use the vars from the init above in here
   }
}

MYNAME.DoStuff.initialize();

Can someone tell me which number (1, 2, or 3) is correct? Are there cases where I would use more than one or all? How about when I need to do DOM references with jQuery? Which way is correct (4, 5, or 6)?

  • 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-17T20:14:47+00:00Added an answer on May 17, 2026 at 8:14 pm

    The var statement will bind the variable to the scope of the current function, in your example var1 is accessible only within initialize.

    Your second example, var2 = 'name'; is simply an assignment made to a -possibly- undeclared identifier.

    This is an anti-pattern, and should be avoided. If the identifier is not resolvable higher in the scope chain, var2 will end up being a property of the global object -an implied global, since most of the time you actually try to avoid globals-.

    Moreover, the new ECMAScript 5 Strict Mode, completely disallows this kind of assignments, please avoid them…

    The third example, this.var3 will create a property in the current object, in your example the this value of initialize will refer to MYNAME.DoStuff because you invoke the initialize method by MYNAME.DoStuff.initialize();.

    In this third example you can access this kind of properties on your linkStuff function, just by this.var3 – if you invoke that linkStuff function properly, e.g. MYNAME.DoStuff.linkStuff();-.

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

Sidebar

Related Questions

I have a project that adds elements to an AutoCad drawing. I noticed that
I am trying to understand the practical difference during the execution of a program
There doesn't seem to be any tried and true set of best practices to

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.