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.

1
  • Well, Abc is easy: .prototype[Abc]. Please see the MDN documentation 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. Commented Dec 8, 2013 at 23:03

1 Answer 1

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.

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

Comments

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.