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

  • Home
  • SEARCH
  • 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 8862889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:50:20+00:00 2026-06-14T15:50:20+00:00

I have adopted a project that has several object literals defined – mostly for

  • 0

I have adopted a project that has several object literals defined – mostly for just structuring the code. Btw, not a full-time front-end developer. The problem is that some of them are defined with the jQuery onready event and some are outside. This seems really problematic to say the least. Would this be considered bad design? I’m assuming so. I have included a snippet of code that shows some of the problems, specifically the the from_inside function within obj2:

<script>
$(document).ready(function(){
  alert('here i am' + obj2.handlers2.background2);  // can access the obj2 value as expected
  alert('make this up: ' + obj2.handlers2.tell_me2());
  var obj={};
  obj.handlers={
    background: 'blue',
    foreground: 'green'
  }
});

var obj2={};
obj2.handlers2={
  background2: 'here i am jt',
  foreground2: 'there you are',
  tell_me2: function(){
    return "these are things";
  }
  ,
  from_inside: function(){
    alert('color from inside jQuery: ' + obj.handlers.background); // problem here! could I get a reference to obj here?
  }
};

obj2.handlers2.from_inside();

</script>

Given how we currently have it, what would be a preferable workaround? It would seem like I could pass a reference of the jQuery object to obj2 in the above but perhaps it might just be simpler to pull all the objects outside of the jQuery on ready even if at all possible.

thx in advance for any advice

edit #1

$(document).ready(function(){
  $(document).on('click','#pairing-comment-submit', function(){
    arc.event_handler.submit_pairing_comment.call(this);
  });
  ... about 50 more of these
});

arc={};
arc.event_handler={
  submit_pairing_comment: function(){
     var id=$(this).data('the-id');
     ....
  },
  ....
}
  • 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-14T15:50:22+00:00Added an answer on June 14, 2026 at 3:50 pm

    You could move the variable assignment outside of the ready function. Here’s a quick refactor:

    var obj={};
    obj.handlers={
      background: 'blue',
      foreground: 'green'
    };
    
    var obj2={};
    obj2.handlers2={
      background2: 'here i am jt',
      foreground2: 'there you are',
      tell_me2: function(){
        return "these are things";
      },
      from_inside: function(){
        alert('color from inside jQuery: ' + obj.handlers.background); // problem here! could I get a reference to obj here?
      }
    };
    
    $(document).ready(function(){
      alert('here i am' + obj2.handlers2.background2);  // can access the obj2 value as expected
      alert('make this up: ' + obj2.handlers2.tell_me2());
      obj2.handlers2.from_inside();
    });
    

    Working example: http://jsfiddle.net/JxS7v/

    The problem you were facing was due to variable scope. Variables are scoped to the function in which they reside, generally speaking. Take a look at this article: http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this

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

Sidebar

Related Questions

In the project I'm developing I've got several common objects that span and associate
I have several methods that I've used previously to accept user input and return
I have a project with a base view controller class, that is inherited by
I have two entities: Project, Employee Employee has primary key {employeeId} + some other
We have several html pages which we are storing as resources within our project.
I have a simple java project (adapted from the example here ), which is
I have adopted an iOS app and am having problem asynchronous requests. We have
I have adapted some code from a great article I found about circle drawing
have written this little class, which generates a UUID every time an object of
I have a project I am working on and it requires drill down UITableView.

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.