With this script I add variables to an object on runtime :
function MyDocument(someDocument)
{
if(!(someDocument instanceof KnownDocumentClass))
throw "Object must be an instance of KnownDocumentClass: " + someDocument;
this.Document = someDocument;
this.Fields = {};
this.updateValues = function()
{
for (var _it = this.Document.iterator(); _it.hasNext();)
{
var _property = _it.next();
try
{
this[_property.getQualifiedName()] = _property.getContent();
}
catch(err)
{
log("Error :"+err);
}
}
}
this.updateValues();
}
So, for example, I can use
var mydoc = new MyDocument(knownjavadoc);
log(mydoc.Creator) // Shows the original content.
This content could have multiple types (some are int
, some String
s and a lot other custom java classes). So it can happen that log(mydoc.SomeProperty)
returns :
PropertyObjectImpl[id=abc123, data=Some Data, type=Node, order=42]
I know, that I could add a function to MyDocument
like
this.getValueAsString = function(name)
{
var _prop = this[name];
if(_prop instanceof PropertyObjectImpl)
return "PropertyObject with ID : " + _prop.getID();
else
return _prop;
}
But for exercise purposes I want to add this function as toValueString()
directly on these properties, so that a call like :
var value = mydoc.SomeProperty.toValueString()
instead of
var value = mydoc.getValueAsString("SomeProperty");
Is this possible?