function foo(name){
this.name = name;
this.bar = function() {
this.name = "John"
console.log("bar's name is " + this.name);
}
}
var blah = new foo("Peter");
console.log(blah.name);//"Peter"
blah.bar();//"bar's name is John"
console.log(blah.name);//"John"
In the example above, how can I make "this", within the function bar, refer to bar, and not its owner, foo? The desired result would be:
var blah = new foo("Peter");
console.log(blah.name);//"Peter"
blah.bar();//"bar's name is John"
console.log(blah.name);//"Peter"