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



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

i tried create object this:

var carousel = { $slider: $('#carousel1 .slider'), panes: carousel.$slider.children().length };

my intentions improve jquery's selector performance caching results of $('#carousel1 .slider') in object property, , maintain code concise , relatively dry.

however, didn't work. when code executed, threw exception when trying parse value of panes, complaining carousel undefined.

this makes sense, since i'd assume carousel isn't declared until assignment statement has been executed. however, i'd avoid resorting this:

var carousel = {}; carousel.$slider = $('#carousel1 .slider'); carousel.panes = carousel.$slider.children().length;

that's not much worse, carousel object have several more properties rely on values of other properties, become verbose.

i tried using this, no avail. may not have been using correctly, or may not valid approach anyway.

is there way properties of object refer other properties of same object, while object still beingness declared?

based on matthew flaschen , casablanca's answers (thanks, guys!), think these versions of actual code i'd end with, based on each approach:

// matthew flaschen var carousel = new (function() { this.$carousel = $('.carousel'); this.$carousel_window = this.$carousel.find('.window'); this.$carousel_slider = this.$carousel.find('.slider'); this.$first_pane = this.$carousel.find('.slider').children(':first-child'); this.panes = this.$carousel_slider.children().length; this.pane_gap = this.$first_pane.css('margin-right'); })();

and

// casablanca var $carousel = $('.carousel'), $carousel_slider = $carousel.find('.slider'), $first_pane: $carousel.find('.slider').children(':first-child'); var properties = { $carousel_window: $carousel.find('.window'), panes: $carousel_slider.children().length, pane_gap: $first_pane.css('margin-right') }; properties.$carousel = $carousel; properties.$carousel_slider = $carousel_slider; properties.$first_pane = $first_pane;

assuming both right (i haven't tested them), it's kind of tough call. think prefer matthew flaschen's approach, since code contained construction more closely resembles object declaration. there's 1 variable created. however, there's lot of this in there, seems repetitive - although may cost pay.

not object literals (this has same value during constructing of literal did before-hand). can do

var carousel = new (function() { this.$slider = $('#carousel1 .slider'); this.panes = this.$slider.children().length; })();

this uses object created anonymous function constructor.

note $slider , panes public, can accessed carousel.$slider, etc.

javascript object properties declaration variable-assignment

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -