2

Can one reference own properties on initialization of object?

I have an object that should be unique and only exist as one so I have no initializer etc.

Using something like this:

var Myobj = {
    property1 : aaa,
    property2 : bbb,
    property3 : {a: self.property1 }
                /*  ^-- is this somehow possible without using a function? 
                        Or a better way to solve / structure this. .init() 
                        best option? */
};

>> Fiddle <<

The real object, (in the real code), has an add function that takes options on what function to use etc. It is sort of a wrapper for "addEventListener" where the point is to be able to remove listener - which require non anonymous function reference.

Function to use within Myobj is specified by string, or numeric key in options to Myobj.add().

Zimzalabim
  • 1,107
  • 1
  • 12
  • 22
  • 1
    No, this won't work without *procedural code*. You can't reference other parts of an object that isn't completely initialized and assigned to a variable yet from within itself. – deceze Apr 11 '13 at 01:49
  • Sorry for the duplication here. My search phrases somehow missed this. – Zimzalabim Apr 11 '13 at 01:51
  • 1
    Don't worry about it-- it's a bit of a hard duplicate to find, since there are lots of other questions with "object", "property", "reference", and "initialization" in them. I *knew* that a duplicate existed (because I've seen come up before) and it still took me a while to find it. – apsillers Apr 11 '13 at 01:53
  • Does my answer not work? – Alex W Apr 11 '13 at 02:03
  • Thanks @deceze, you gave some insight in your comments here. I'll go for *procedural code* in a init() function. – Zimzalabim Apr 11 '13 at 02:30

2 Answers2

1

Object literals remain undefined until the closing }, so you'd either have to make property3 a function or make the whole object a function.

var Myobj = {
    property1 : 'aaa',
    property3 : function (){ return { a : this.property1 } }
};

or

var MyObj = function(){
  this.property1 = 'aaa',
  this.property3 = { a: this.property1 }
}
var MyObjInstance = new MyObj();

This has been asked a lot of times: Access properties while declaring object?

Community
  • 1
  • 1
Coin_op
  • 10,568
  • 4
  • 35
  • 46
0

It works if you use a placeholder array:

var prop = new Array();
var Myobj = {
    property1 : 'aaa',
    property2 : 'bbb',
    property3 : {a: prop}
};
prop.push(Myobj.property1);

window.alert(Myobj.property3.a);

http://jsfiddle.net/y2XwC/1/

Alex W
  • 37,233
  • 13
  • 109
  • 109
  • Thanks. Interesting approach. I do not believe I'm going to use it in this case, but I'll remember it down the line as a design option if it should be appropriate. – Zimzalabim Apr 11 '13 at 02:07
  • This makes `Myobj.property3.a[0] == 'aaa'`. Not the same as `Myobj.property3.a == 'aaa'`. – deceze Apr 11 '13 at 02:09
  • @deceze It works without needing the index. – Alex W Apr 11 '13 at 02:11
  • 1
    Depends how you define "works". It *is* an array, so things like `Myobj.property3.a.substr(1)` won't work. – deceze Apr 11 '13 at 02:14
  • The advantage would be that arrays are mutable, so he could change the value later: http://jsfiddle.net/y2XwC/3/ – Alex W Apr 11 '13 at 02:17
  • Maybe the answer here is that if the two values need to stay in sync, they should not be separate values to begin with. But if they're just initialized to the same value but can diverge later, arrays are of no advantage. – deceze Apr 11 '13 at 02:20
  • He asked how he could do it without a function. This is my attempt. Sorry if it's completely useless. – Alex W Apr 11 '13 at 02:20