Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would defining `that = this` help me create private variables/members?

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());
like image 924
Edward Tanguay Avatar asked Dec 15 '10 09:12

Edward Tanguay


People also ask

What are some benefits of making member variables private?

By making the variable a private data member, you can more easily ensure that the value is never negative. On the other hand, if the variable is public, another class could change it to a negative value which can cause other parts of the code to crash. Save this answer.

How do you define a private variable?

In general, private variables are those variables that can be visible and accessible only within the class they belong to and not outside the class or any other class. These variables are used to access the values whenever the program runs that is used to keep the data hidden from other classes.

How do you make a variable private?

Alternatively, we may also use the “this” keyword to make method (function) calls to stick to the main method itself which thus makes the variables private. The main idea for using the “this” keyword is just to make things directly visible that is making methods directly accessible.

How do you define a private variable in a classroom?

Class variables that are declared as private can not be referred to from other classes, they are only visible within their own class. It is considered better programming practice to use private rather than public class variables, and you should aim to do this in the remainder of the course.


1 Answers

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/

like image 102
Gabriele Petrioli Avatar answered Nov 11 '22 13:11

Gabriele Petrioli