In Java Script its rather easy to extend existing objects and prototypes with new functions. However, if you try that with medini meta classes, you will fail: extending the meta model is by intention forbidden.
// XXX this will fail with
Metamodel.projectmodel.PJPackage.foo = function() {
alert("cool");
}
In contrast it is possible to extend native Java Script prototypes to add new functions and by doing so, make the script nicer and the language more powerful. An example:
/**
* Some String extension: Formats the string with two parameters.
*
* @memberOf String
* @param {Object} arg1 first argument
* @param {Object} arg2 second argument
* @returns {String}
* @see java.lang.MessageFormat
*/
String.prototype.format = function(arg1, arg2) {
// just delegate the call to Java
return java.text.MessageFormat.format(this, arg1, arg2);
};
var count = 33;
var kind = "bean";
alert("You have {0} of {1} in your basket".format(count, kind));
With this extension its possible to create formatted messages similar to the console API. And there are many more use cases like that they one could imagine.
If you have any idea or similar script snippet, just post it here.
Happy Scripting
Jan