0

I've tried searching for but perhaps I am describing it incorrectly since I'm not having any luck finding results.

I want to do this:

    XS.prototype.Abc=function $Def(a,c){ ... }

But I would like XS, ABC, and $Def to be variables ( dynamic)

For XS since this is in the global scope I think that I can use window[] like this:

var myindex = 'XS';
window[myindex].prototype.Abc=function $Def(a,c){ ... }

I read over What is the difference between call and apply? but I still can't figure out if it would be possible to use call or apply to do the other two as variables.

Community
  • 1
  • 1
Steve Brown
  • 1,336
  • 4
  • 16
  • 20
  • Well, `Abc` is easy: `.prototype[Abc]`. Please see the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators) for more information. Regarding `$Def`, it's not possible (and the name of a function expression is only accessibly from within a function, so you might not even have to name the function). `.call` and `.apply` have nothing to do with that. – Felix Kling Dec 08 '13 at 23:03

1 Answers1

1

You can use [] notation on any object:

window[ctorName].prototype[methodName] = function() { };

However, AFAIK, there is no non-evil way to set a function's name dynamically.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964