2

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

2
  • What you should probably do is define these functions elsewhere, then assign them within the if conditions if (object_type === 'HTMLCollection') { HTMLCollection.prototype.func_name = yourFunction; } — that'll greatly improve readability. Commented May 29, 2015 at 6:46
  • By "dynamism" you mean using variables? Commented May 29, 2015 at 7:09

2 Answers 2

1

In your case you can simply do

window[object_type].prototype[func_name] = function(){...

But be careful that you seem to be engaged in modifying objects you don't own. There's probably a better possible design for your application.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I would prefer not to break scope from my initial object. I'm not sure if this is possible.
0

Without going into details about what are you trying to accomplish or whether it's a good idea or there are better ways to do it, just based on your posted code, all you need to do is define "some code" as a funtion and assign it to whatever you want:

window.myClass = function(){
  function someCode() { /* some code */ }

  var object_type = getObjectType();
  if (object_type === 'NodeList')
  {
     NodeList.prototype.func_name = someCode;
  }

  if (object_type === 'HTMLCollection')
  {
     HTMLCollection.prototype.func_name = someCode;
  }
}

But you don't really need the if statement, because you can just do

window.myClass = function(){
  function someCode() { /* some code */ }

  var object_type = getObjectType();
  window[object_type].prototype.func_name = someCode;
}

4 Comments

Thanks for this. I would not be comfortable with extending the window object (as your second code block suggests). Is there a way I can use this same bracket notation for things within the scope of my original object e.g. [object_type].prototype.func_name?
You're not extending the window object. You're adding a function to the prototype of something which is already on the window object.
So does that mean the code is encapsulated and there isn't any chance of collisions?
There is a chance of collisions if someone else adds the same method to the prototype of the same DOM class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.