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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:35+00:00 2026-06-14T17:19:35+00:00

if anyone can help me I am having a problem with closures and retrieving

  • 0

if anyone can help me I am having a problem with closures and retrieving a private variable declared that is a element selected by id using jquery. I have a module like so:

var package = package || {};

(function() {
        if(!package.slider)
                package.slider = {};

        function Slider() {
        }

        // --------- /Public Members --------- //
        Slider.prototype = {
            init: function(){
                _setEvents();
            }
        };

        // --------- Public Members --------- //


        // --------- Private Members --------- //
        var shoePreview = $('#shoe-preview');  /*somehow this variable does NOT get recognised even though I have it in the DOM.*/

        function _setEvents(){
            $.subscribe('getAllPerspectives', function(event){ _getAllPerspectives() });
            $.subscribe('showPreview', _show);
            $.subscribe('hidePreview', _hide);
        }

        function _getAllPerspectives(){
            _getAngleShoeImage(helper.getCurrentAngle());
            $.each(constants.angles, function(key, val){
                if(val != helper.getCurrentAngle()){
                    _getAngleShoeImage(val);
                }
            });
        }

        function _getAngleShoeImage(angle){
            var shoeImage = shoePreview;
            shoeImage.html('<p>Hello World</p>');

        }


        var that = new Slider();
        package.slider = that;

    })();
$(document).ready(function(){

    $.each(package, function (key, val){
        val.init();
    });
});

Now If you looked in the code, there is a variable called shoePreview, which I assume would not be accessible to anything outside the module.

Now somewhere in the code later I will call some methods which will trigger _getAngleShoeImage where I have variable inside called shoeImage set to the private shoePreview variable.

The problem is that once I try to use jquery’s html() call it seems to not recognise what the shoePreview variable is.

I’ve tested this with substituting var shoeImage = $(‘#shoe-preview’), then calling html() with the desired markup and it works fine.

This problem is so small but it’s driving me nuts. Am I writing the var shoePreview = $(‘#shoe-preview’); in the wrong way or there’s something more to it. Any help will be appreciated.

Thanks.

  • 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-14T17:19:36+00:00Added an answer on June 14, 2026 at 5:19 pm

    Here is only one problem:
    (function(){hackhackhack()})() literally mean – run the closure, it means you tried to access DOM before it ready, you just need to setup the var after init, not in main function,

    var package = package || {};
    
    (function() {
        if(!package.slider)
                package.slider = {};
    
        function Slider() {
        }
    
        // --------- /Public Members --------- //
        Slider.prototype = {
            init: function(){
                //New function here
                _setVars();
                _setEvents();
            }
        };
    
        // --------- Public Members --------- //
    
    
        // --------- Private Members --------- //
        //make it accessible for other proto functions;
        var shoePreview;
    
        function _setVars() {
           shoePreview=$('#shoe-preview'); // now it will be set when init() run not when object proto initialised
        }
        function _setEvents(){
            $.subscribe('getAllPerspectives', function(event){ _getAllPerspectives() });
            $.subscribe('showPreview', _show);
            $.subscribe('hidePreview', _hide);
        }
    
        function _getAllPerspectives(){
            _getAngleShoeImage(helper.getCurrentAngle());
            $.each(constants.angles, function(key, val){
                if(val != helper.getCurrentAngle()){
                    _getAngleShoeImage(val);
                }
            });
        }
    
        function _getAngleShoeImage(angle){
            var shoeImage = shoePreview;
            shoeImage.html('<p>Hello World</p>');
    
        }
    
    
        var that = new Slider();
        package.slider = that;
    
    })();
    $(document).ready(function(){
    
        $.each(package, function (key, val){
            val.init();
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if anyone can help with a jQuery problem I am having. I
Can anyone help with an odd Ruby Gem problem I'm having - note I've
I am having a problem reading inputs, can anyone help me. Each line of
I'm having a problem with this question can anyone help? an explanation would be
I am having problem in understanding locality of reference. Can anyone please help me
Wondering if anyone can help me with this slight problem i'm having. I am
Anyone that can help me out will be very much appreciated. I seem to
Can anyone help. I am using a formview in VS 2005. I have different
I would like to know if anyone can help me out with a problem
I am having problem to access an element in my file.xul using DOM methods

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.