views:

167

answers:

3

Simple as that, can we emulate the "protected" visibility in Javascript somehow?

+3  A: 

Do this:

/* Note: Do not break/touch this object */
...code...

Or a bit of google found this on the first page:

http://blog.blanquera.com/2009/03/javascript-protected-methods-and.html

Aiden Bell
What this guy is doing is just ducktyping to see if he's 'this' has the same method as the calling function... it cannot guarantee that's the same guy who's calling.
Pablo Cabrera
What I'm trying to say is that I don't think that this way you can really protect your 'protected' stuff :P
Pablo Cabrera
@Pablo, I agree totally ... but if you want that behavior as concrete semantics then JS isn't the language you want :P
Aiden Bell
+1  A: 

What could that possibly mean? You don't have classes.

I suppose you could analyze caller to determine whether it meets some set of criteria for being permitted to call a method. This will be hideously inefficient and your criteria will always be spoofable.

chaos
Not exactly true. In Javascript _everything_ is an object. You define classes using an active syntax, instead of a passive one. Composition vs. declaration.
Christopher
@Christopher ... You are wrong, just because it is prototypical doesnt mean the prototypes are classes! Are they also interfaces?
Aiden Bell
Well, okay. But what I mean is that you don't have a namespace of entity categorizations to which an object can be known to belong or not belong, which is what protected visibility normally keys on. You can't tell an inheritor from something that wants to look like an inheritor.
chaos
Still, without proper classes we can have the concept of private members using JS Variable scoping tho
Pablo Cabrera
I think languages that leave the semantics out and allow implementations (like decorators and such) are cool. Just a pain in the arse when you need a 'standard' effect.
Aiden Bell
+2  A: 

Sure you can. Here's another example.

Josh Stodola
Snap! :) ......
Aiden Bell
Wow. That is psycho.
chaos
Actually, it's CHAOS ;)
Josh Stodola