PDA

View Full Version : Explanation please



gizzmo
18 Oct 2007, 1:22 AM
Hi

Can someone explain the difference in code:


MyObject = function() {
function private() {
alert("private");
}
};

MyObject.prototype = {
public : function() {
private();
}
}
and...


MyObject = function() {
function private() {
alert("private");
};

this.public = function() {
private();
};
};

Thanks

SeaSharp
18 Oct 2007, 5:20 AM
In the first example, public method "public()" cannot access the private method according to the test code I just ran.

gizzmo
19 Oct 2007, 11:27 AM
Yeah I know. but I want an explanation why. I don't understand why the public function cannot reach the private in the first example.

harley.333
19 Oct 2007, 5:13 PM
A third option is:


MyObject = function() {
function private() {
alert("private");
}
};

MyObject.prototype.public = function() {
private();
};


Perhaps that gives you a hint as to why Option #1 doesn't work.