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