Hi,

> It does not appear that Prototype forges any sort of accessor for new
> classes, but rather just keeps all vars available as "public"
> properties, accessible via instanceName.propertyName, correct?

That's correct.

> IF that is the case, and I have behavior dependent on a property
> change, how do I handle that in the "Prototype" way?

There's no "Prototype" way to do it, it's not part of the feature set
Prototype provides.

Until very recently, there was no standard way to have private
properties only accessible via accessors. There were various ways of
emulating that behavior (like Crockford's solution[1] of using
variables within closures), at various costs (in Crockford's case,
lots of functions are duplicated for *every* instance of the object,
which can become untenable); and there were various proprietary
enhancements to JavaScript (from Mozilla and otherwise) that provided
it; but no _standard_ way to do it.

The recent ECMAScript 5th edition[2] provides one via the
Object.defineProperty function that has getter and setter functions
triggered any time that property is gotten or set; the spec is pretty
hard to read, but Resig's done a nice summary[3]. So when browsers
support it, you'll be able to use that.

Until then, it's a matter of roll-your-own. Crockford's approach works
perfectly well with Prototype's class stuff, with the caveat (again)
that all of your getters and setters are duplicated for every
instance. (For _just_ getters and setters, if you're not going to have
thousands of objects, that should be okay.) Example:
http://jsbin.com/odofi4/2

Of course, that still requires (as shown in the example) that people
access the property through functions, which you may or may not think
is a good thing (I've heard both sermons). The nice thing about the
accessors added to ECMAScript 5 is that (when they're implemented)
that decision is up to the developer rather than being a limitation of
the language.

[1] http://javascript.crockford.com/private.html
[2] http://www.ecma-international.org/publications/standards/Ecma-262.htm
[3] http://ejohn.org/blog/ecmascript-5-objects-and-properties/

FWIW & HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Aug 6, 9:29 pm, Raconteur <mythosracont...@gmail.com> wrote:
> Hi all,
>
> I am building a rather massive HTML5 / OOJS project, and happened upon
> a nice custom event framework that makes use of Prototype.  I was
> rolling my own OOJS framework, but liked what I saw and thought I'd
> try integrating Prototype into my work.
>
> My first question (probably of MANY!) is in regard to properties and
> accessors.
>
> It does not appear that Prototype forges any sort of accessor for new
> classes, but rather just keeps all vars available as "public"
> properties, accessible via instanceName.propertyName, correct?
>
> IF that is the case, and I have behavior dependent on a property
> change, how do I handle that in the "Prototype" way?  What I had done
> before is all of my instance vars were declared with "var" to make
> them private, and then exposed with mandatory accessors (getVarName,
> and setVarName) if you wanted them publicly available.  Then I could
> easily override the setter in a superclass with my own implementation
> where I needed to have additional code run when a property was set.
>
> How would I do this in the proper Prototype fashion?
>
> Thanks!
>
> Chris

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to