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

The Archive Base Latest Questions

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

I was reading Douglas Crawford’s piece on creating private variables in javascript classes. In

  • 0

I was reading Douglas Crawford’s piece on creating private variables in javascript classes.

In it he says you have to state that = this in order to “make the object available to private methods”. However, I was able to build an example which has private members, private methods and public methods without defining that = this:

function Form(id_code) {

    //private variable
    var id_code = id_code;
    var color = '#ccc';

    //private method
    function build_style_attribute() {
        return 'style="background-color:'+color+'"';
    }

    //public method
    this.render = function() {
        return '<div '+build_style_attribute()+'>'+id_code+'</div>';
    }
}

var formModules = new Form('modules');

$('p#test').html(formModules.render());

What would specifying that = this allow me to do which this example does not already do?

Added:

Thanks @Gaby, so this is how I understand it: as the above example shows, I have access to private variables without using that=this but it does give me access to public variables as shown here:

function Form(id_code) {
    that = this;

    //private variable
    var id_code = id_code;
    var color = '#ccc';

    //public variable
    this.weight = 'bold';

    //private method
    function build_style_attribute() {
        //this will not work with either "weight" or "this.weight"
        return 'style="background-color:'+color+'; font-weight:'+that.weight+'"';
    }

    //public method
    this.render = function() {
        return '<div '+build_style_attribute()+'>'+id_code+'</div>';
    }
}

var formModules = new Form('modules');

$('p#test').html(formModules.render());
  • 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-18T12:41:34+00:00Added an answer on May 18, 2026 at 12:41 pm

    By convention, we make a private that
    variable. This is used to make the
    object available to the private
    methods
    .

    This is a workaround for an
    error in the ECMAScript Language
    Specification which causes this to be
    set incorrectly for inner functions
    .

    function Test() {
        var that = this;
        
        function wrongprivate(){
         return this;
        }
        
        function rightprivate(){
         return that;
        }    
        
        this.check= function (){
         console.log( wrongprivate() );
         console.log( rightprivate() );
        }
        
    }
    
    var test= new Test();
    test.check();
    // will output 
    // window
    // object{}
    

    Live at http://www.jsfiddle.net/BpmQ3/1/

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

Sidebar

Related Questions

I remember reading in Douglas Crockford's Javascript the Good Parts book that there is
Reading through this question on multi-threaded javascript, I was wondering if there would be
I'm reading Douglas Crockfords Javascript: The Good Parts , I just finished the regular
I was reading Douglas Crockford's web page, JavaScript: The World's Most Misunderstood Programming Language
I'm not a Javascript person normally, but I've been diving in, reading Douglas Crockford's
I just started reading on Douglas Crockford's Javascript The Good parts where he explains
I'm just reading about Prototypes in JavaScript and Douglas Crockford offers and excellent way
Reading this question I found this as (note the quotation marks) code to solve
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading this post has left me wondering; are nightly builds ever better for a

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.