Can you push into an array literal from within?
-
The syntax of your "object inside array" is invalid. The value within an object literal can be an expression, but it's not clear what `{tag:"label" id="note_title"}` means. – RobG Apr 13 '17 at 00:56
-
Related: [Self-references in object literal declarations](http://stackoverflow.com/q/4616202/218196) – Felix Kling Apr 13 '17 at 02:40
-
@Felix Kling, thanks, I was thinking about posting this in my question because it is similar to what I am looking for, but I am specifically interested in the array literal. var foo = { a: 5, b: 6, init: function() { this.c = this.a + this.b; return this; } }.init(); – Rick Apr 13 '17 at 03:19
1 Answers
No. You cannot do that. In Javascript, the items
array (what you call "the parent array") does not exist in the local scope until the literal declaration is fully processed by the interpreter so you cannot .push()
into it within the literal declaration.
If you want to dynamically modify it based on code conditions, you have to do that after the literal declaration has finished.
You can declare a method in the literal declaration that will modify the items
array. But, if that method is invoked in the middle of the static declaration, then items
will not yet be defined because its definition has not yet finished. That method needs to be called later after the array literal declaration has finished.
But, in your self-invoking scheme from within the literal declaration, items
will not be defined so you will get an error.
You can have a self invoking expression that runs and returns some value and that value can then be the value of some property within your literal declaration. That's just a regular Javascript expression and any literal declaration value can be computed via an expression if you want. In ES6, you can even use a computed property name. But, the parent array
symbol does not exist yet until after the literal declaration is done being processed so you can't access that parent symbol from within an expression that is evaluated during the parsing of the literal declaration.
Explained another way, in your above code, you essentially have:
let items = x;
where x
is a big object literal you've declared.
Javascript will compute the entire x
before it assigns anything to items
. So, you can't be conditionally adding things to items
while x
is being evaluated.

- 683,504
- 96
- 985
- 979
-
thanks for the clarification on array scope, within the context of array literal declarations, that was a big help! Is there a better way of achieving what I am trying to accomplish? – Rick Apr 13 '17 at 01:32
-
@Arrow - I don't really understand exactly what you're trying to accomplish. The usual way to set something dynamically into a literal declaration is to just assign/insert it after the declaration using regular Javascript that can modify the array/object or create a method that you call after the declaration. – jfriend00 Apr 13 '17 at 02:00
-
I was able to use a custom event to trigger the literal into firing after it is fully processed by the interpreter. However, I lose all references to "this" as undefined. I thought since the declaration has been defined I would have access to its properties? – Rick Apr 13 '17 at 17:28
-
@Arrow - I would have to see what code you are using in order to know how to help you with your `this` issue. – jfriend00 Apr 13 '17 at 19:11