Add a variable to a class instance at runtime: Difference between revisions
Content added Content deleted
(autohotkey example) |
|||
Line 213: | Line 213: | ||
=={{header|J}}== |
=={{header|J}}== |
||
If you assign a value to the name which references a property of a class instance, that name within that instance gets that value. |
|||
Simple assignment will add variables to classes. |
|||
<lang j> C=:<'exampleclass' |
<lang j> C=:<'exampleclass' NB. this will be our class name |
||
V__C=: 0 NB. ensure the class exists |
|||
V__C=: 0 |
|||
OBJ1=:conew 'exampleclass' NB. create an instance of our class |
|||
OBJ2=:conew 'exampleclass' NB. create another instance |
|||
V__OBJ |
|||
V__OBJ1,V__OBJ2 NB. both of our instances exist |
|||
0 |
0 |
||
W__OBJ1 NB. instance does not have a W |
|||
W__C |
|||
|value error |
|value error |
||
W__OBJ1=: 0 NB. here, we add a W to this instance |
|||
W__C=: 0 |
|||
W__OBJ1 NB. this instance now has a W |
|||
W__OBJ |
|||
0 |
|||
0</lang> |
|||
W__OBJ2 NB. our other instance does not |
|||
|value error</lang> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |