Scope Test _this

JavaScript

console.clear();
var PersonClass = function PersonClass(_name, _age)
{
    var _this=this;
    this.name=_name;
    this.age=_age;

    console.log("created "+this.name+":"+this.age);

    this.changeAge = function(num)
    {
        console.log("changing age for:" + _this.name);
        _this.age = num;
    }.bind(this);

    this.getAge = function()
    { 
        console.log(_this.name+":"+_this.age);
        return _this.age;
    }.bind(this);

    this.getAge2 = function()
    { 
        console.log(this.name+":"+this.age);        
        return this.age;
    };
};

// comments indicate what value is displayed in console
var john = new PersonClass("john",1);  // john:1
var sue = new PersonClass("sue",2);    // sue:2

john.getAge(); // sue:2
john.getAge2(); // john:1

john.changeAge(10); // sue
sue.getAge(); // sue:10
sue.getAge2(); // sue:10
john.getAge(); // sue:10
john.getAge2(); // john:1