Heya Matt :-)
First, I''m basing my example code on the inheritance model outlined in
my
blog post:
http://www.someelement.com/2007/03/multiple-inheritance-with-prototypejs.html
So with that said, here is some example code. The premise is simple:
encapsulate class definitions within the constructor. I explain the
difference between truly instance only members and "prototype-static"
members in that blog post as well, as I use this model to achieve not only
truly private variables within a class definition, but also truly "instance
only" public members - which is to say, they are only available on
instances
of a class and cannot be called via "
someClass.prototype.someMethod()" like
you can do with the very common class model used by many people.
var fooClass = Class.create();
fooClass.prototype = {
initialize: function(_name) {
//private variables
var type = "test";
var name = _name;
//private methods
function sayName() {
alert(name);
}
//public instance methods and member getters/setters
this.getType = function() {
return type;
};
this.getName = function() {
return name;
};
this.setName = function(value) {
name = value;
};
this.sayName = function() {
sayName();
};
}
};
//Static members
Object.extend(fooClass, {
someStaticMethod: function() {
alert("static only, does not have access to any instance members or private
variables");
}
});
//Inheritance with method overriding
var newClass = Class.create();
Object.inherit(newClass, fooClass);
Object.extend(newClass.prototype, {
initialize: function(_name) {
//base class construction
newClass.base.call(this, _name);
//override sayName
var oldSayName = this.sayName.bind(this);
this.sayName = function() {
if (this.getName() == "Bob")
alert("Robert");
else
oldSayName();
};
}
});
On 8/24/07, Matt Foster
<mattfoster01-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> Hey Everyone,
>
> I was wondering if anyone knows of a clean way to handle truly
> private variables. I am not talking about the _underscore naming
> convention for defining what should be private, i am looking for true
> blue encapsulated data.
>
> Cheers,
> Matt
>
>
> >
>
--
Ryan Gahl
Manager, Senior Software Engineer
Nth Penguin, LLC
http://www.nthpenguin.com
--
Architect
WebWidgetry.com / MashupStudio.com
Future Home of the World''s First Complete Web Platform
--
Inquire: 1-262-951-6727
Blog: http://www.someElement.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---