- Found at http://code.google.com/p/polymor...
- Language
- JavaScript
- Tags
- js
- Favorited By
Polymorphic function example in JS
1 var PolyFunc = polymorph(
2 function(a,b,c) {
3 return "Three arguments version -- any types";
4 },
5
6 {i: Number, str: String},
7 function(i,str) {
8 return "Number and string passed";
9 },
10
11 {re: RegExp},
12 function(re,a) {
13 return "RegExp and something else passed";
14 },
15
16 {f: Function, b: Boolean},
17 function(f,b) {
18 return "Function and boolean passed";
19 },
20
21 {f: Function, i: Number},
22 function(f,i) {
23 return "Function and number passed";
24 }
25 );
26
27 alert(PolyFunc(1,2,3)); // "Three arguments version -- any types"
28 alert(PolyFunc(1,"qq")); // "Number and string passed"
29 alert(PolyFunc(function() {}, true)); // "Function and boolean passed"
30 alert(PolyFunc(function() {}, 1)); // "Function and number passed"
31 alert(PolyFunc(/a/, 1)); // "RegExp and something else passed"
32 alert(PolyFunc(/a/, "str")); // "RegExp and something else passed"
Comments
-
principiante 2010-07-23
lol
Sign in to leave a comment.

