The problem with your approach is that only that instance of Date
will get that metadata. Any other instances will not, unless you have some other function that automatically augments it to the instance. Something like:
function dateWithMetaData(meta){
var aDate = newDate();
aDate._my_ns_metadata = meta;
return aDate;
}
var d = dateWithMetaData("some meta-data comes here");
But then, where would you place that function so that it is immediately accessible anywhere for any instance of Date
? Where else but the Date.prototype
of course. Adding something to an object's prototype makes it available to all instances of it. Thus you can do this:
// Modifying the prototype
Date.prototype.setMetaData = function(meta){
this._my_ns_metadata = meta;
}
// Now any instance of Date has setMetaData
var d = new Date();
d.setMetaData("some meta-data comes here");
var x = new Date();
x.setMetaData("some meta-data comes here");
By design, JS allows modification of native objects. However, it's not really advisable to modify objects you don't own. You might unintentionally break stuff.