Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active November 3, 2016 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/66a4ce97cbef735afbb56df6c9b46e35 to your computer and use it in GitHub Desktop.
Save tommcfarlin/66a4ce97cbef735afbb56df6c9b46e35 to your computer and use it in GitHub Desktop.
[JavaScript] Using Configuration Files for Class Properties
{
"Foo": {
"property_type": [{ "property_name": "property_value" }]
}
}
{
"Foo": {
"PropertyType": [{ "property_name": "property_value" }]
},
"Bar": {
"StringCollection": {
"number_names": [ { "0": "zero" }, { "1": "one" }]
},
"baz": {
"Baz": {
"string": [{ "twitter_handle": "@tommcfarlin" }]
}
}
}
}
/*
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