Skip to main content
Much better title, and removed the confusing use of "prototype"
Source Link
Ixrec
  • 27.7k
  • 15
  • 84
  • 87

Cleanly generating several 0-airty JavaScript function prototypingfunctions with slightly different bodies

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 prototype thosemake some kind of template for this set of functions?

ThisThat 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 kind of prototyping 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.

JavaScript function prototyping

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 prototype those functions?

This 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 kind of prototyping 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.

Cleanly generating several 0-airty JavaScript functions with slightly different bodies

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.

improved formatting and style; changed mentions of JScript to JavaScript
Source Link
amon
  • 135.9k
  • 27
  • 295
  • 386

Jscript JavaScript function prototyping

Say I have a bunch of JscriptJavaScript functions similar to:

message = new Array (4);
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 prototype those functions?

This 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]XMLHttpRequest[5] or XMLHttpRequest[2] 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 kind of prototyping with Jscript. Any help will be appreciatedJavaScript.

Thank you!

EDIT 1

Just to be clear -, I'm using those functions for XmlHttpRequest.onstatechangeXmlHttpRequest.onstatechange so those are called without parameterswithout any parameters (something like: xhr[0].onstatechange=F0; xhr[1].onstatechange=F1;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 withinwithin those function themselves.

Jscript function prototyping

Say I have a bunch of Jscript functions similar to:

message = new Array (4);
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 prototype those functions?

This 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 kind of prototyping with Jscript. Any help will be appreciated.

Thank you!

EDIT 1

Just to be clear - I'm using those functions for XmlHttpRequest.onstatechange so those are called without 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.

JavaScript function prototyping

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 prototype those functions?

This 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 kind of prototyping 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.

Added details to the question
Source Link
Alex
  • 21
  • 3

Say I have a bunch of Jscript functions similar to:

message = new Array (4);
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 prototype those functions?

This 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 kind of prototyping with Jscript. Any help will be appreciated.

Thank you!

EDIT 1

Just to be clear - I'm using those functions for XmlHttpRequest.onstatechange so those are called without 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.

Say I have a bunch of Jscript functions similar to:

message = new Array (4);
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 prototype those functions?

This 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 kind of prototyping with Jscript. Any help will be appreciated.

Thank you!

Say I have a bunch of Jscript functions similar to:

message = new Array (4);
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 prototype those functions?

This 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 kind of prototyping with Jscript. Any help will be appreciated.

Thank you!

EDIT 1

Just to be clear - I'm using those functions for XmlHttpRequest.onstatechange so those are called without 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.

Source Link
Alex
  • 21
  • 3
Loading