Add a variable to a class instance at runtime: Difference between revisions
Add a variable to a class instance at runtime (view source)
Revision as of 19:02, 1 December 2018
, 5 years agoMoved jq entry into correct alphabetical order.
(Added Go) |
m (Moved jq entry into correct alphabetical order.) |
||
Line 663:
e.foo = 1
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 in effect 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|Julia}}==
Line 748 ⟶ 758:
Its value is 'rosetta'
</pre>
▲=={{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 in effect 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|Lingo}}==
|