-
-
Save tommcfarlin/66a4ce97cbef735afbb56df6c9b46e35 to your computer and use it in GitHub Desktop.
[JavaScript] Using Configuration Files for Class Properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Foo": { | |
"property_type": [{ "property_name": "property_value" }] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Foo": { | |
"PropertyType": [{ "property_name": "property_value" }] | |
}, | |
"Bar": { | |
"StringCollection": { | |
"number_names": [ { "0": "zero" }, { "1": "one" }] | |
}, | |
"baz": { | |
"Baz": { | |
"string": [{ "twitter_handle": "@tommcfarlin" }] | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The following code provides configuration for dynamically instantiating classes. | |
The first class, `Foo`, shows how to create a basic class. It has a single property | |
where `property_type` refers to the data type of the value. This might be a primitive | |
or it may be a reference to another class. The value may be a single value (as | |
with `Baz->twitter_handle`), or it may be an array (as with `Bar->number_names`). | |
`Bar` has two properties, a `StringCollection` with the variable name `number_names` and the | |
array of values following that. And it depends on a reference to an instance of `Baz` which is | |
instantied within the `Bar` configuration. | |
`Baz` maintains a single property which is a string identified by `twitter_handle` having the | |
value of `@tommcfarlin`. | |
There are other considerations not shown here (such as visibility modifiers, etc.). But the idea | |
of instantiating classes at runtime with dynamic data can be done using a configuration file so | |
we aren't constantly hopping through classes to change values (which is helpful during development | |
time). | |
*/ | |
{ | |
"Foo": { | |
"PropertyType": [{ "property_name": "property_value" }] | |
}, | |
"Bar": { | |
"StringCollection": { | |
"number_names": [ { "0": "zero" }, { "1": "one" } ] | |
}, | |
"baz": { | |
"Baz": { | |
"string": [{ "twitter_handle": "@tommcfarlin" }] | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment