2

Say I have a bunch of JavaScript functions similar to:

message = ["000", "111", "222", "333"];
function F0(){
 alert (message[0]);
}
function F1(){
 alert (message[1]);
}
function F2(){
 alert (message[2]);
}
function F3(){
 alert (message[3]);
}

Is there a way to make some kind of template for this set of functions?

That is, to declare something like:

function F<T>(){
 alert (message[T]);
}

The reason is quite simple. In my solution, I have much more complex functions performing the same action (they are callback functions for a few XMLHttpRequest objects). Each time I make a change, it needs to be reflected across all functions – something I’m looking to avoid. From within the function, I cannot know whether it was called by XMLHttpRequest[5] or XMLHttpRequest[2], therefore I created a few similar functions (for each XMLHttpRequest object). I’m looking for a way to prototype those functions, so I’ll keep the logic in one place. So far, I haven’t found a way to achieve that with JavaScript.

Just to be clear, I'm using those functions for XmlHttpRequest.onstatechange so those are called without any parameters (something like: xhr[0].onstatechange = F0; xhr[1].onstatechange = F1;). I'm looking for a way to prototype just the function and than create a few "instances" of it, differentiated by an index within those function themselves.

Ixrec
27.7k15 gold badges84 silver badges87 bronze badges
asked Mar 26, 2016 at 11:30
2
  • Note that JScript is a Microsoft-specific JavaScript variant, but is not the same as JavaScript. I answered assuming you really meant JavaScript. Commented Mar 26, 2016 at 12:21
  • Yes, that right. Commented Mar 26, 2016 at 12:28

2 Answers 2

2

Yes, use function arguments:

function F(T) { alert(message[T]); }

If you need a function that given this "template" can subsequently be invoked without arguments, we can use a closure – returning a function from within a function:

function makeF(T) {
 return function F() { alert(message[T]); };
}
// var F1 = makeF(1);
// etc.

The C++ templates or Java/C# generics are like compile-time functions which are mostly used for creating a family of types. JavaScript doesn't really have a compile-time type system, or even a compile time, so those concepts don't apply here. We can therefore supply arguments at run time to pretty much the same effect.

answered Mar 26, 2016 at 12:17
2
  • First, hello amon and thank you. Second, I'm using those functions for XmlHttpRequest.onstatechange so those are called without parameters (something like: xhr[0].onstatechange=F0; xhr[1].onstatechange=F1;) so unfortunately, it doesn't fit. I'm looking for a way to prototype just the function and than create a few "instances" of it, differentiated by an index within those function themselves. Commented Mar 26, 2016 at 12:28
  • @Alex ah, that makes sense. See update. Commented Mar 26, 2016 at 12:30
2

Another option is function.bind which will return a function with the arguments bound to it:

var f1 = F.bind(null, 1)

See - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

answered Mar 26, 2016 at 15:41
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.