5 ms·
In your example cygx, this.foo is a public member variable. As you say, it is possible to have private member variables but this implies some constrains. The f
by chmike 15y ago
In your example cygx, this.foo is a public member variable.
As you say, it is possible to have private member variables but this implies some constrains.
The following example illustrate how one may have private and public members as well as private and public methods and how to access them.
function AClass(foo)
{
this.foo = foo; // public member variable
var bar = "bar"; // private member variable
function fun1() // private method
{
bar = this.foo; // private and public var access
}
this.fun2 = function () // public privileged method
{
bar = this.foo; // private and public var access
}
}
A public method defined with prototype doesn't allow to access private member variables. This is the difference between privileged methods and public methods. The privileged method can access private methods because of closure.
This is all very different from other OOP languages. I just add these for clarification. It may be helpful to some readers.
By convention one may also add an underscore in front of private members and function identifiers to make the difference clearly visible in the code.
Would Object.create also inherit private functions and members ?
- cygx 15y agoWould Object.create also inherit private functions and members ? No, it would not - these so-called private members and methods have nothing to do with inheritance, but rather lexical closure. However, as the super constructor is called, priviledged methods will be present and still have access to the private variables. See also http://stackoverflow.com/a/1441692/48015 http://stackoverflow.com/a/1441692/48015 for a more comprehensive overview over different access levels.
- chmike 15y agoThank you very much. It's the first time I see this pattern and a clear specification. I used privileged methods everywhere instead of shared public methods based on this article: http://javascript.crockford.com/private.html http://javascript.crockford.com/private.html The missing infos in the StackOverflow answer are how do one instantiate such class and access it's methods and public and shared members ? var obj = new MyObj() ? obj.publicSharedVar ?