Introduction: This is a new series of articles I would like to start. Main objective is to share basic scripting knowledge in a quick way and to benefit from the power of Java Script and OCL. The format is to just post script code with inline documentation, no more no less. That way I hope you get the most out of it without reading long introduction text. Full stop.
Here is the first one about:
// 1. define a simple object, in principle a name/value mapping
var object = {
"id" : 15,
"name" : "bar"
};
// 2. dynamically add another attribute (name/value mapping) to the object
object["description"] = "foo bar";
// 3. iterate over all mappings of the object
for (var name in object) {
console.log("{0} is {1} (type {2})", name, object[name], typeof object[name]);
}
// 4. log the object in JSON (JSON: JavaScript Object Notation) so basically as script code
console.log("{0}", uneval(object));
// 5. dynamically delete an attribute
delete object["description"]
console.log("{0}", uneval(object));
Happy Scripting
Jan