in javascript whay use " var that = this "

Posted by tarek11011 on Programmers See other posts from Programmers or by tarek11011
Published on 2011-03-17T06:39:45Z Indexed on 2011/03/17 8:17 UTC
Read the original article Hit count: 162

Filed under:

hi i am new with javascript

What is the benefit of using this line

var that = this

An example

function Person( firstname, lastname, age ) {

    this.firstname = firstname;

    this.lastname = lastname;

    this.age = age;

    getfullname = function() {

        return firstname + “ “ + lastname;
    };

    var that = this;


    this.sayHi = function() {

        document.write( “Hi my name is “ + getfullname() + “ and I am “ + that.age + “years old.”);

    };
}

thanks

© Programmers or respective owner

Related posts about JavaScript