{"id":11,"date":"2007-11-15T10:00:18","date_gmt":"2007-11-15T08:00:18","guid":{"rendered":"http:\/\/kinsey.no\/wp\/?p=11"},"modified":"2010-04-08T22:28:58","modified_gmt":"2010-04-08T20:28:58","slug":"javascript-function-overloading","status":"publish","type":"post","link":"https:\/\/kinsey.no\/blog\/index.php\/2007\/11\/15\/javascript-function-overloading\/","title":{"rendered":"Javascript function overloading"},"content":{"rendered":"<p>I was reading a post yesterday about function overloading in javascript, and the suggested solution had quite a performance overhead when used with multiple signatures.<br \/>\nIn addition, it only used the number of arguments, and not their types and order to distinguish between the signatures.<\/p>\n<p>For some reason I woke up early this morning with a solution to this.<br \/>\nThe following code will do all of the above and has the same overhead for all number of signatures.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/returns an overloadable method\r\nFunction.CreateOverloaded = function(){\r\n    var overloads = [];\r\n    \/\/creates the overloaded method\r\n    var func = function(){\r\n        var sign = [];\r\n        for (var i=0;i &lt; arguments.length; i++) {\r\n            sign.push(typeof arguments[i]);\r\n        }\r\n        sign = sign.join();\r\n        var func = overloads[&quot;(&quot; + sign + &quot;)&quot;];\r\n        if (!func) {throw &quot;No matching signature&quot;;}\r\n        return func.apply(this, arguments);\r\n    };\r\n\r\n    \/\/method for adding a function with a spesific signature\r\n    func.AddSignature = function(func, sign){\r\n        if (overloads[&quot;(&quot; + sign + &quot;)&quot;]){throw &quot;Duplicate signature&quot;};\r\n        overloads[&quot;(&quot; + sign + &quot;)&quot;] = func;\r\n    };\r\n    return func;\r\n}\r\n<\/pre>\n<p>To test it you can use the following code<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar myFunc = Function.CreateOverloaded();\r\nmyFunc.AddSignature(function(){\r\n    alert(&quot;test1&quot;);\r\n}, &quot;&quot;);\r\nmyFunc.AddSignature(function(var){\r\n    alert(var);\r\n}, &quot;string&quot;);\r\nmyFunc.AddSignature(function(integer){\r\n    alert(integer + &quot;x 5 = &quot; + (integer * 5));\r\n}, &quot;number&quot;);\r\nmyFunc.AddSignature(function(int1, int2){\r\n    alert(int1 * int2);\r\n}, &quot;number,number&quot;);\r\nmyFunc.AddSignature(function(string, int){\r\n    alert(string + &quot; equals &quot; + int);\r\n}, &quot;string,number&quot;);\r\n\r\nmyFunct();\r\nmyFunc(&quot;test2&quot;)\r\nmyFunc(3);\r\nmyFunc(3, 5);\r\nmyFunc(&quot;five&quot;, 5);\r\n<\/pre>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>I was reading a post yesterday about function overloading in javascript, and the suggested solution had quite a performance overhead when used with multiple signatures. In addition, it only used the number of arguments, and not their types and order to distinguish between the signatures. For some reason I woke up early this morning with [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34,1],"tags":[21,8],"_links":{"self":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":10,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}