Jump to content

Add a variable to a class instance at runtime: Difference between revisions

(autohotkey example)
Line 213:
 
=={{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' NB. this will be our class name
V__C=: 0 NB. ensure the class exists
V__C=: 0
OBJOBJ1=: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
W__OBJ1 NB. instance does not have a W
W__C
|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}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.