Can a JavaScript object property refer to another property of the same object?

Not with object literals ( this has the same value during constructing of the literal that it did before-hand). But you can do.

Not with object literals (this has the same value during constructing of the literal that it did before-hand). But you can do var carousel = new (function() { this. $slider = $('#carousel1 .

Slider'); this. Panes = this. $slider.children().

Length; })(); This uses an object created from an anonymous function constructor. Note that $slider and panes are public, so can be accessed as carousel. $slider, etc.

This is essentially a more verbose version of the OP's second example. – casablanca Jul 4 '10 at 3:25 @casablanca, not really. He asked, "Is there a way for properties of an object to refer to other properties of the same object, while that object is still being declared?", and that's exactly what this is.

With my example, carousel can use previously defined properties, but it is not accessible from the outside until it's fully constructed. With his code, that wasn't the case. And it does use this (which the OP said he wanted), rather than having to repeat carousel.

He said he was planning on adding more properties, which could make this approach shorter too. – Matthew Flaschen Jul 4 '10 at 3:29 I guess you're right about that. – casablanca Jul 4 '10 at 3:44 Thanks, Matthew!

I actually wasn't entirely set on using this, it just seemed like it might have been applicable in this situation. I can't think of a more concise approach than the one you suggested, though, in spite of the numerous references to this. I appreciate it.

– Bungle Jul 4 '10 at 3:50 1 @Bungle, you only have use this to refer to public properties. For instance, if $slider were private, you could replace the first this. $slider with var $slider, and the second with $slider.

– Matthew Flaschen Jul 4 '10 at 4:23.

Unfortunately, no. The {} syntax initiates creation of a new object, but until the object is created, it is not assigned to the carousel variable. Also, the this value can only change as a result of a function call.

If your "several more properties" are all going to depend only on slider, then you could get around with something like this: var slider = $('. Slider'); var carousel = { panes: slider.children.length(), something: slider. Something_else, // ... }; carousel.

Slider = slider.

Thanks, casablanca! I do like your approach for cases when most of the object properties rely on just one or two other variables. For my use case, unfortunately, that's not the case, so I think Matthew Flaschen's approach ends up being slightly more elegant. Thanks again.

– Bungle Jul 4 '10 at 3:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions