Is it possible to reference object within the same object?

Posted by fudgey on Stack Overflow See other posts from Stack Overflow or by fudgey
Published on 2010-05-13T17:45:29Z Indexed on 2010/05/13 17:54 UTC
Read the original article Hit count: 220

Filed under:
|

I've been messing around with jQuery plugin code and I'm trying to put all of my common variables into a single object for easy access. I have included a few examples below on how I've done this, but I'm wondering how others deal with this problem.

Lets say I have this

var x = function(options){
 var defaults = {
  ulist   : $('ul#list'),
  listLen : $('ul#list').children().length
 }
 $.extend(options, defaults);
 // do other stuff
}

What I'm trying to do is use the ulist object in as a base, then find the number of li's

I guess I could do this:

var x = function(options){
 var defaults = {
  ulist   : $('ul#list'),
  listLen : 0
 }
 defaults.listLen = defaults.ulist.children().length;
 $.extend(options, defaults);
 // do other stuff
}

or this:

var x = function(options){
 var defaults = {
  ulist : $('ul#list')
 };
 var defaults2 = {
  listLen : defaults.ulist.children().length
 }
 $.extend(defaults, defaults2);
 $.extend(options, defaults);
 // do other stuff
}

The above code samples are just thrown together, and only meant to get the idea across to you. Anyway, is there a better way to do this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery