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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:54:14+00:00 2026-05-14T05:54:14+00:00

var Dog = function() { var _instance = ‘hello world’; return function() { console.log(this._instance);

  • 0
var Dog = function() {

    var _instance = 'hello world';

    return function() {
        console.log(this._instance);
    }
} (); //note that it is self invoking function

var l = new Dog(); //#> undefined 

In the above case I was expecting an output of:

‘hello world’

Why is this._instance not accessing the the variable which should be accessible by virtue of closure? I tested this in FF and am getting undefined.

  • 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-14T05:54:14+00:00Added an answer on May 14, 2026 at 5:54 am

    You don’t assign _instance to the object, it’s just a closure variable, and should be accessed without using this:

    var Dog = function() {
    
        var _instance = 'hello world';
    
        return function() {
            console.log(_instance);
        }
    } (); //note that it is self invoking function
    
    var l = new Dog();
    

    I’d probably write it like so instead:

    var Dog = (function() {
    
        var defaults = {
           name: 'Rags'
        };
    
        var Dog = function (options) {
            // Take options as a constructor argument, this
            // allows you to merge it with defaults to override
            // options on specific instances
            this.setOptions(options);
        };
    
        Dog.prototype = {
           // Common methods for Dogs here
           setOptions: function (options) {
              // Declare all variables in the beginning of the method because
              // JavaScript hoists variable declarations
              var key = null;
              // First assign all the defaults to this object
              for ( key in defaults) {
                 this[key] = defaults[key];
              }
              // Now override with the values in options:
              if (options && options.hasOwnProperty) {
                 for ( key in options ) {
                    this[key] = options[key];
                 }
              }
           }
        };
    
        return Dog; // Return the constructor method 
    } ()); // wrap the self-invoked function in paranthesis to visualize that
           // it creates a closure
    
    var buster = new Dog({name: 'Buster'}),
        unnamed = new Dog();
    
    alert(buster.name); // Alerts 'Buster'
    alert(unnamed.name); // Alerts 'Rags'
    

    Note that I have not tried to compile the above code, so it might contain a few mistakes. Nothing JsLint can’t handle though!

    You might want to consider adding filtering to the setOptions method so that it doesn’t assign properties you don’t want, or filter out methods etc declared in the options-parameter.

    Additionally, if you use JQuery, or similar library, there are (often) utility functions for merging objects, making it trivial to write the setOptions-method:

    function setOptions (options) {
       // I assume JQuery here
       // true as the first argument gives us a recursive merge
       var mergedOptions = $.extend(true, defaults, options);
       for (var key in mergedOptions ) {
          if(this.checkAllowedProperty(key, typeof(mergedOptions[key])) {
             this[key] = mergedOptions[key];
          }
       }
    }
    
    /**
     * This method checks if propertyName is an allowed property on this object.
     * If dataType is supplied it also checks if propertyName is allowed for
     * dataType
     * @return true if propertyName, with type dataType, is allowed on this object,
     * else false
     */
    function checkAllowedProperty (propertyName, dataType);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 461k
  • Answers 461k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've found a solution for this problem, using FLARManager. http://words.transmote.com/wp/flarmanager/flarmanager-documentation/#flarManagerSettings_inverted… May 16, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer Short answer is no, if you want it to exist… May 16, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer Here is an alternative approach: Find the first timestamp >=… May 16, 2026 at 12:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.