If function have the scope ,they should be executed within that scope but here i think its different .see the code
function foo() {
var privateVal = "Private Val";
this.publicVal = "Public Val";
var privateAlert = function (str) {
alert(str + this.publicVal);
alert(str + privateVal);
}
this.Run = function () //see here
{
privateAlert("Private Call: ");
this.publicAlert = privateAlert;
this.publicAlert("Public Call: ");
privateAlert = this.publicAlert;
privateAlert("Private Call: ");
this.publicAlert("Public Call: ");
}
}
var bar = new foo();
bar.Run();
when the new object is created run() becomes the private method of an object or the method tht only belongs to the var bar.That method shouldn't be able to execute the privateAlert() function from within it ,since function has the scope it can only get executed from within the function it has been declared but not from the method tht now belongs to some other object. clarify this plz.