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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:04:18+00:00 2026-05-27T17:04:18+00:00

I have this code (JSFiddle) var OBJ = function(){ var privateVar = 23; var

  • 0

I have this code (JSFiddle)

var OBJ = function(){
    var privateVar = 23;
    var self = this;

    return {
        thePrivateVar : function() {
          return privateVar;
        },  

        thePrivateVarTimeout : function() {
            setTimeout(function() { alert(self.thePrivateVar()); } , 10);
        }
    }

}();

alert(OBJ.thePrivateVar());

OBJ.thePrivateVarTimeout();

This is an abstraction of a real problem I’m having.

So – I would expect the call to OBJ.thePrivateVarTimeout() to wait 10 and then alert with 23 (which I want it to access through the other exposed method).

However self doesn’t seem to be setting correctly. When I am setting self = this it appears that this isn’t a reference to the function but a reference to the global object. Why is this?

How can I make the public method thePrivateVarTimeout call the other public method thePrivateVar?

  • 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-27T17:04:18+00:00Added an answer on May 27, 2026 at 5:04 pm
    var OBJ = (function(){
        var privateVar = 23;
        var self = {
            thePrivateVar : function() {
              return privateVar;
            },  
    
            thePrivateVarTimeout : function() {
                setTimeout(function() { alert(self.thePrivateVar); } , 10);
            }
        };
    
        return self;
    
    }());
    

    this === global || undefined inside an invoked function. In ES5 it’s whatever the global environment is, in ES5 strict it is undefined.

    More common patterns would involve using the var that = this as a local value in a function

    var obj = (function() {
      var obj = {
        property: "foobar",
        timeout: function _timeout() {
          var that = this;
          setTimeout(alertData, 10);
    
          function alertData() {
            alert(that.property);
          }
        }
      }
    
      return obj;
    }());
    

    or using a .bindAll method

    var obj = (function() {
      var obj = {
        alertData: function _alertData() {
          alert(this.property);
        }
        property: "foobar",
        timeout: function _timeout() {
          setTimeout(this.alertData, 10);
        }
      }
    
      bindAll(obj)
    
      return obj;
    }());
    
    
    /*
        bindAll binds all methods to have their context set to the object
    
        @param Object obj - the object to bind methods on
        @param Array methods - optional whitelist of methods to bind
    
        @return Object - the bound object
    */
    function bindAll(obj, whitelist) {
        var keys = Object.keys(obj).filter(stripNonMethods);
    
        (whitelist || keys).forEach(bindMethod);
    
        function stripNonMethods(name) {
            return typeof obj[name] === "function";
        }
    
        function bindMethod(name) {
            obj[name] = obj[name].bind(obj);
        }
    
        return obj;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have weird problem with this code $('img').hover(function(){ var $cap = $(this).parent().find('.cap'); window.setTimeout(function(){$cap.stop(true,false).animate({bottom:164},500)},500); },function(){
I have the following code: http://jsfiddle.net/maniator/vTjW8/ var createChartTemplate = function() { return { chart:
I have this code... function a(options) { for (var item in options) { if
I have this code in jquery: var ratio; $(<img/>).attr(src, $(img).attr(src)).load(function() { ratio = this.width
All of this is code is here: http://jsfiddle.net/yrgK8/ I have a news section which
I have following code (live: http://jsfiddle.net/xyZY8/1/ ): var str = '<div>hello</div><ul><li>Some text...</li>' + '<li>second
I have the following code (I am using the jQquery libary): var obj =
I have this code: var max1box = document.getElementById('length'), max2box = document.getElementById('width'), max1 = 100,
I have the following code at JSfiddle: http://jsfiddle.net/ACG2D/ $(document).ready(function() { $('.testThis').click(function() { $('.regexValidation').each(function() {
I have this code which works. $(document).ready(function(){ $('form').live('submit', function(){ // have to do it

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.