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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:38:41+00:00 2026-05-25T12:38:41+00:00

Consider this first Javascript snippet, which only uses var and this to declare scope:

  • 0

Consider this first Javascript snippet, which only uses var and this to declare scope:

var Bro = function() {
    var self = this;
    this.storyName = "story"; // public
    var storyType = "cool"; // private

    // private function
    var composeStory  = function() {
        return storyType + ' ' + self.storyName;
    };

    // privileged function
    this.tellStory = function() {
        console.log(composeStory() + ' bro!');
    };
};

Compared to this, which uses a return statement, and modifies the scope somewhat:

var Bro = function() {
    var self = this;
    this.storyName = "story"; // private?
    var storyType = "cool"; // private

    // private function
    var composeStory = function() {
        return storyType + ' ' + self.storyName;
    };

    // privileged variables/functions
    return {
        storyName: this.storyName,
        tellStory: function() {
            console.log(composeStory() + ' bro!');
        }
    };
}

My question then is:

  1. When would you use the second method over the first, or is this just a stylistic thing?
  2. Why is this.storyName in the second example private, even though I set it on this?
  3. I had to declare var self = this; in order to get access to the this scope inside both private functions. How do Javascript frameworks make the this object accessible inside their functions?
  4. In the second example, the storyName property in the return statement could access the this scope properly. Why can it do that, when the private function could not?
  • 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-25T12:38:42+00:00Added an answer on May 25, 2026 at 12:38 pm

    There is no such thing as “private” and “public” in Javascript. What you are seeing is the difference between properties on an object and closure captured variables. Closure capture variables can be used to hide state but they are hiding it on a different object attached to the functions created in the closure scope, not on the object being returned, as in other languages. With that in mind I will answer your questions,

    1) I wouldn’t user either form but they are stylistically similar, both are using closure capture. Note, however, the first uses the constructor pattern and the second uses a factory pattern. The first will only work if you use the new keyword prior to calling Bro the second will ignore the object created by new in favor of the object created by the object literal, but you can call it safely without prefixing it with new.

    2) storyName is not private, it is set on a different object. When a function is called prefixed by new a fresh object is created and passed in as the “this” parameter. This is the object you are setting by “this.storyName”. This object is then discarded in favor of the object you create in the literal. You don’t see storyName because it wasn’t set on the object returned.

    3) You can capture this in a closure variable like self or use bind on the function to bind this to the original this. I prefer closure capture myself.

    4) The this is still in scope because you are executing in the constructor function.

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

Sidebar

Related Questions

Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable
Consider this javascript: var values = { name: Joe Smith, location: { city: Los
Consider this definition object: var map = { 'Username': { 'user-name': 'userName', 'first-name': 'fName',
Consider the following JavaScript code which has a recursive boom function function foo() {
Consider this JavaScript Code: <script type=text/javascript> function loop(Message){ document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + Message +
Consider this Asp.net page code: <head runat=server> <title></title> <script type=text/javascript> function showhide(master, detail) {
Consider a string array shaped like this: string[] someName = new string[] { First,
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider the following document <html> <body> This is some content <script type=text/javascript src=verySlowToRespond.js></script> This
Consider this scenario: Using Javascript/E4X, in a non-browser usage scenario (a Javascript HL7 integration

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.