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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:47:09+00:00 2026-06-15T10:47:09+00:00

there are several function will have to get the dom $$(‘.box’)[0] , I don’t

  • 0

there are several function will have to get the dom $$(‘.box’)[0] ,
I don’t want to let box to be a glabal var, and I don’t want to let js seek the
dom every time . and I don’t want to run $$(‘.box’)[0] when user not run these functions.
how to store the box var?
enter image description here

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
<script type="text/javascript">
    /* ------- 
        there are several function will have to get the dom $$('.box')[0] ,
        I don't want to let box to be a glabal var, and I don't want to let js seek the 
        dom every time . and I don't want to run $$('.box')[0] when user not run these functions.
        how to store the box var?
    ------- */
    function a(){
        var box = $$('.box')[0];
        //...
    }
    function b(){
        var box = $$('.box')[0];
        //...
    }
    //...
</script>
<div class="box"></div>
  • 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-15T10:47:10+00:00Added an answer on June 15, 2026 at 10:47 am

    you should really use a closure then.

    (function(scope){
    
        var box = box document.getElement('.box'); // same as $$()[0], returns first match
    
        scope.a = function(){
            return box;
        };
    
        scope.b = function(){
            box;
        };
    
    }(this)); // whatever object, eg, window or a namespace
    
    box; // reference error.
    this.a(); // box element object
    

    box will remain private and also static, i.e. it wont get refreshed anymore.

    you can do it so it’s referenced and cached once when needed:

    (function(scope){
        var box;
    
        scope.a = function(){
            box = box || document.getElement('.box');
            return box;
        };
    
        scope.b = function(){
            // alt syntax;
            box || (box = document.getElement('.box'));
            return box;
        };
    
    }(this)); // whatever object, eg, window or a namespace
    

    this way, calling either method will cache box and make it available for any of the methods in the closure.

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

Sidebar

Related Questions

There are several other questions about this topic that I have gone through, but
I have the following jQuery: // get position on new/removed items function get_new_position_for_all_items() {
I have several tables that get JOINed together to form a table with columns
I have several ajax requests that return different values (pseudocode ahead) var foo =
I have a situation where I want to use several server side controls, which
I have this function: <SCRIPT language=JavaScript> <!-- function go_there() { var where_to= confirm(Do you
I have a ticket system on my site. There are several divs which show
I have an event handler, but I want to make sure that it will
There are several threads on this here at SO but I didn't find one
There are several variant questions on this on SO, but I did not find

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.