JavaScript let's you do proper encapsulation using closures like so:

function Person(name) {
  this.setName = function(value) {
    name = value;
  }

  this.getName = function() {
    return name;
  }
}

There's a memory cost to this, though: every "instance" of the Person
"class" will get it's own set of methods so that:

var joe = new Person('Joe');
var john = new Person('John');

joe.setName === john.setName;
// -> false

You can even use factory functions and drop the "new" and "this"
keywords altogether:

function personFactory(name) {
  var person = {};
  person.setName = function(value) {
    name = value;
  }

  person.getName = function() {
    return name;
  }
  return person;
}

Experience shows that either of the above solution aren't acceptable,
performance wise, for medium to large applications. There's just too
much time spent in GC.

Best,

Tobie

On Mar 10, 3:27 am, kangax <[email protected]> wrote:
> On Mar 9, 9:50 am, webbear1000 <[email protected]> wrote:
>
>
>
> > I'm interested in your opinions on how I'm handling getters and
> > setters in classes. Can you see any problems with my approach and what
> > trouble might I be getting myself into?
>
> > The huge body of my programming work has been with ASP.NET in VB and
> > C#. So I'm used to classic OOP rather than prototype inheritance. With
> > that in mind, I'm trying to semi-replicate getters and setters
> > thus ...
>
> > ----------------------------------------------------------
> > this.property = function(value){
> >     if(arguments.length == 0){
> >         return localVariable;
> >     }else{
> >         localVariable = value;
> >     }}
>
> > ----------------------------------------------------------
>
> > So if no arguments are passed, the function becomes a getter or a
> > setter if there are arguments.
>
> > What do you think?
>
> I don't see how prototypal inheritance would not allow you to
> implement property accessors : )
>
> It's easy to do so with either plain javascript:
>
> function Person(name) {
>   this.setName(name);}
>
> Person.prototype.getName = function() {
>   return this.name;}
>
> Person.prototype.setName = function(name) {
>   this.name = name;
>
> }
>
> or using abstraction that Prototype.js provides:
>
> var Person = Class.create({
>   initialize: function(name) {
>     this.setName(name);
>   },
>   getName: function() {
>     return this.name;
>   },
>   setName: function(name) {
>     this.name = name;
>   }
>
> });
>
> I'm not a fan of one-method-accessor API. I like `getXXX`, `setXXX`
> notation more, as it seems to be more descriptive and less error-
> prone. You are obviously free to use whichever you thinks suits
> better.
>
> --
> kangax



--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to