Louis,

Your error is that you should not be using the "new" keyword before
"Class.create()"

Class.create() is a helper function that essentially just returns a
function. Note, in js, all named functions are considered constructors.
Obviously, a function that has no prototype properties and sets not instance
properties via "this.something = something;" are probably not all that
useful.

So, while you can create a new instance of "Class.create"... since it's a
"returning function", and really has no prototype of its own beyond the
standard "Function" prototype, you're not achieving much.

When you assign the _result_ of calling Class.create, which again, returns a
function, you've created a named function (at your desired "namespace"
location, that maps calls to itself to a prototype property called
initialize.

So...

FooBar.UI = Class.create({

...is what you really wanted. And then "var something = new FooBar.UI()"
actually maps to FooBar.UI.prototype.initialize.

Note, you don't need to use Class.create() to create "classes" (and really,
there are no classes in js, simply prototypes and instances).

The following is "plain old javascript" that sets up a "class" called
"someClass".

function someClass() {
> this.blah = "foo";
> }
>
> function.prototype.someMethod = function() {...};
>
> var something = new someClass();








On Mon, May 11, 2009 at 5:26 PM, louis w <louiswa...@gmail.com> wrote:

>
> Josh could you post an example of how you do it? I am interested in
> all of the different approaches.
> Thanks.
>
> On May 11, 6:12 pm, Josh Powell <seas...@gmail.com> wrote:
> > If I only need one of something, I go with an object, otherwise if I
> > need more then one I go with a class-like structure.. though I don't
> > use Prototypes Class method, so I'll let someone that does answer that
> > part of the question.
> >
> > Josh Powell
> >
> > On May 11, 1:44 pm, louis w <louiswa...@gmail.com> wrote:
> >
> > > I have always set up my Javascript using objects like below:
> >
> > > if (!FooBar_Old) var FooBar_Old = {};
> > > FooBar_Old.UI = {
> >
> > >         test_str : 'it works'
> >
> > >         , test: function() {
> > >                 console.log(this.test_str);
> > >         }
> >
> > > };
> >
> > > // FooBar_Old.UI.test();
> >
> > > However recently I have been wanting to convert these to real classes
> > > to take advantage of the initialize constructor. I took a first pass
> > > at this, and can't get it working.
> >
> > > This post is two questions:
> > > #1 Which method do you use in your javascript programming Objects or
> > > Classes?
> > > #2 What am I doing wrong with my class declaration below?
> >
> > > // Class Based (Doesn't work)
> >
> > > if (!FooBar) var FooBar = {};
> > > FooBar.UI = new Class.create({
> >
> > >         test_str : 'it works'
> >
> > >         , initialize: function(options) {
> > >                 // Whatever
> > >         }
> >
> > >         , test: function() {
> > >                 console.log(this.test_str);
> > >         }
> >
> > > });
> >
> > > // FooBar.UI.test();
> >
>

--~--~---------~--~----~------------~-------~--~----~
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-scriptaculous@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