Add a variable to a class instance at runtime: Difference between revisions
Content added Content deleted
(comment on "incorrect" comment.) |
(jq) |
||
Line 554: | Line 554: | ||
e.foo = 1 |
e.foo = 1 |
||
e["bar"] = 2 // name specified at runtime</lang> |
e["bar"] = 2 // name specified at runtime</lang> |
||
=={{header|jq}}== |
|||
jq's "+" operator can be used to add a key/value pair (or to add multiple key-value pairs) to an existing object at runtime, but |
|||
jq is a functional programming language, and objects themselves cannot be altered. Thus it may be helpful to introduce a variable, since the value of a variable can be updated. For example: |
|||
<lang jq>{"a":1} as $a | ($a + {"b":2}) as $a | $a |
|||
</lang>Thus the value of $a has undergone the desired transition, that is, its final value is {"a":1, "b":2}. |
|||
A Javascript-like syntax can also be used to add (or update) a key, for example:<lang jq>$a|.c = 3 |
|||
# or equivalently: |
|||
$a|.["c"] = 3</lang> |
|||
=={{header|LOLCODE}}== |
=={{header|LOLCODE}}== |