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 / com
www.crowdersoftware.com

On May 21, 9:39 pm, blechler <[email protected]> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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 [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