I have to make a bunch of .prototype declarations within a function and would like to add some dynamism to reduce the size of my code.
Here is some pseudo code for what I need to do:
window.myClass = function(){
var object_type = getObjectType();
if (object_type === 'NodeList')
{
NodeList.prototype.func_name = function(){
//some code
}
}
if (object_type === 'HTMLCollection')
{
HTMLCollection.prototype.func_name = function(){
//the same code again
}
}
}
I would like to change this so I can make these declarations dynamic, kind of like this:
window.myClass = function(){
var object_type = getObjectType();
object_type.prototype.func_name = function(){
//some code
}
}
Is this possible?
EDIT
I forgot to mention that I would love to keep all my functions within the scope of window.myClass
if (object_type === 'HTMLCollection') { HTMLCollection.prototype.func_name = yourFunction; }— that'll greatly improve readability.