Hi,

Sorry, just realized that while my previous answer is hopefully
helpful in terms of why what you were doing doesn't work, it *doesn't*
help you alias methods. :-) A couple of options for you:

If you want to change that code as little as possible, you can alias
when the `initialize` method is called:

    var baz = Class.create({
      initialize: function() {
          this.bar = this.foo;
      },
      foo: function() {
        alert('I am foo and bar!');
      }
    });

...but that's really ugly, and each instance gets its own `bar`
property instead of sharing the `bar` on the prototype (not that that
matters a *lot*).

So here's what I'd do instead:

First off, I'm not a fan of anonymous functions like the ones in your
class. Yes, those functions are anonymous, even though they don't
immediately seem it; it's just that they're bound to properties that
have names. I won't go into it here (I've done so elsewhere[1]), but
the first thing I'd do is change to using named functions:

    var baz = Class.create((function(){
      var pubs = {};

      pubs.foo = foo;
      function foo() {
        alert('I am foo and bar!');
      }

      return pubs;
    })());

That looks a bit more complicated, but now your `foo` function really
is named `foo` (in addition to being assigned to a `foo` property).
Here's how that works:

1. Instead of passing an object literal into `Class.create`, we call a
function and feed its return value into it. That's what all the
parentheses are about. :-)

2. Within the function, we define `foo` using a function declaration
(rather than a function expression).

3. We use the `pubs` object to create the thing we'll pass into
`Class.create`.

4. For each public function, right where we declare it, we assign it
to `pubs` (so we don't forget).

With this structure, aliases are easy:

    var baz = Class.create((function(){
      var pubs = {};

      pubs.foo = foo;
      pubs.bar = foo;  // <== Alias `foo` as `bar`
      function foo() {
        alert('I am foo and bar!');
      }

      return pubs;
    })());

For more about named vs. anonymous functions, check out this post[1]
on my blog.

[1] http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html

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


On May 22, 8:16 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> When you use object literal notation, the right-hand side of an
> initialization (the bit after the colon [:]) is evaluated when the
> literal is evaluated, and then the *value* of the result is used to
> initialize that property on the object created by the literal (the
> options object you're passing into `Class.create`). So `this.foo` is
> evaluated in the context of the code calling `Class.create`, and its
> *value* is used to initialize `bar`.
>
> This may be easier to understand if we take a simpler example:
>
>     var alpha, beta;
>
>     alpha = 5;
>     beta = {
>         key: alpha
>     };
>
> What's the value of `beta`'s `key` property? Is it `alpha`, or `5`?
> The answer of course is that it's `5`; when the object literal was
> evaluated, the *value* of `alpha` was used to initialize `key`, not a
> reference to `alpha`.
>
> Similarly:
>
>     var gamma, delta;
>
>     gamma = {
>         key: 5
>     };
>     delta = {
>         key: gamma.key
>     };
>
> Again the value of `delta`'s `key` property is `5`; there's no link to
> `gamma` or its `key` property.
>
> So let's go back to your `bar` initialization:
>
>     var baz = Class.create({
>       bar: this.foo,
>       foo: function() {
>         alert('I am foo and bar!');
>       }
>     });
>
> What's the value of `bar`? Right, whatever the value of `this.foo` is
> as of when you call `Class.create`. The `this` has nothing to do with
> an instance of your class, it's whatever `this` is for the code that's
> *calling* `Class.create`.
>
> HTH,
> --
> T.J. Crowder
> Independent Software Consultant
> tj / crowder software / comwww.crowdersoftware.com
>
> On May 21, 9:39 pm, blechler <lech...@gmail.com> wrote:
>
>
>
>
>
> > Hi all,
>
> > Given the code:
> > -------------------------------------------
> > var baz = Class.create({
> >   bar: this.foo,
>
> >   foo: function() {
> >     alert('I am foo and bar!');
> >   }
>
> > });
>
> > myObj = new baz();
> > -------------------------------------------
> > I would expect that:
> > myObj.bar();
>
> > would pop up the alert, but it turns out that myObj.bar is undefined.
> > Must I write it like:
>
> >   bar: function() { this.foo(); }
>
> > Any insight would be appreciated.
>
> > --
> > 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 
> > athttp://groups.google.com/group/prototype-scriptaculous?hl=en.
>
> --
> 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 
> athttp://groups.google.com/group/prototype-scriptaculous?hl=en.

-- 
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