Jump to content

Would this code have created a protected status?


jekillen

Recommended Posts

Hello:

 

I believe I have established a design pattern that would establish what other oop languages

call protected. It would apply to the variable value and functions returned from _firstObj() to

first in _secondObj().

 

I am really a hobbyist who has been learning bit by bit over a number of years. I don't remember

seeing anything in texts like O'Reilly's Javascript: The Definitive Guide, or similar Apress publications

that specifically address this issue in javascript.

 

I was wondering if anyone here with more extensive knowledge could evalutate this.

I have posted this query not only for my own edification, but for anyone who could

benefit from this, who might visit for the learning experience.

 

In this I was experimenting to see if I could set the 'private' variable someVal

by using the _firstObj function both as an object construtor and as a normal function

call.

 

I have also attached a fully standalone html page that can be used to play with this.

 

function _firstObj(a) { var someVal = a; var setVal = function(d) { someVal = d return 0; } var show = function() { return {'value':someVal} } return {'value':someVal,'show':show,'set':setVal}; }function _secondObj() { var first = new _firstObj('This') // object construtor this.setOtherObjProperty = function(a) { return _firstObj(a) // as a normal function call } this.showFirst = function() { first.show() } this.run = function(c,d) { return first[c](d) } }

protectedObjects.html

Link to comment
Share on other sites

Javascript doesn't really have a way to handle protected members, because it doesn't have classic inheritance. There's not a way to say that a certain property can be set in the class or object that defines it, but not in things that extend that class or object. There are ways to approximate private variables, but you can't do protected.

Link to comment
Share on other sites

Just to clarify (and not to be argumentative)

 

What I meant by 'private' and 'protected' is that a private variable is one that cannot be set or read from outside of the object instance.

And a protect variable is one that can only be set or read, if there is code to set or read it, in an enclosing object instance;

 

If I do

function example()

{

var x = 'this is what x is';

this.y = 'this is visible and setable from outside';

}

then:

var firstExample = new example()

 

Then :

for(var x in firstExample)

{

if(firstExample.propertyIsEnumerable(x))

{

alert(x+' in firstExample is '+firstExample[x])

}

}

'x' in firstExample won't come up in an alert

That is want I understand to be 'private' in a javascript context.

 

Now, since, in the example I originally used, the firstObj instance is

used to create a value for a 'private' variable in the secondObj instance.

And the second object instance has code to set and read the firsObj instance:

it emulates a protected status in firstObj instance.

 

I also do php programming and have written OOP code and have an understanding

of private and protected in that context.

Link to comment
Share on other sites

I got protected backwards, protected variables can be accessed by classes that extend the original, I said they couldn't. A protected member is like a private member, but it is allowed to also be accessed by objects that extend the original. Private members cannot. The presence of setter/getter methods doesn't change the visibility of a variable. Public, private, and protected variables can all be changed by setter/getter methods, but only public variables can be accessed directly. Private variables can only be directly accessed inside the class where they are defined, and protected variables can be accessed inside the class they are defined as well as any class that extends the parent.

That is want I understand to be 'private' in a javascript context.

Sort of, that's the best that Javascript has. Since it's prototype-based instead of object-oriented (and lacks things like inheritance necessary for OO), it also doesn't have object member visibility like public, private, and protected. What you're describing is a closure, and is the closest that Javascript gets to private variables. The variable is only defined in a single scope, it is not defined outside that scope. The scope stays in existence for as long as the object is in existence.

Now, since, in the example I originally used, the firstObj instance isused to create a value for a 'private' variable in the secondObj instance.And the second object instance has code to set and read the firsObj instance:it emulates a protected status in firstObj instance.

Only superficially, your setOtherObjProperty function doesn't change any existing variables, for example, it simply returns a new object that is not connected with any other object. Calling setOtherObjProperty and then calling showFirst, for example, will always show 'This' even if you tried to change the value with setOtherObjProperty. You cannot directly access the someVal variable to either get or set it inside the secondObj object, it is not visible in that scope at all. You can use the methods to set or get the value, but you cannot access that variable directly.So, as far as Javascript goes, all object properties are public members. It's possible to emulate private members by using a closure, and protected doesn't make sense in Javascript because you can't inherit in the first place. You can use getter/setter methods to set any kind of object property or "private" closure variable. Setter/getter methods are the only way to access closure variables from outside the closure scope.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...