diff options
author | matthewsotoudeh <matthewsot@outlook.com> | 2015-05-10 17:49:29 -0700 |
---|---|---|
committer | matthewsotoudeh <matthewsot@outlook.com> | 2015-05-10 17:49:29 -0700 |
commit | b5a7c325aa6102fc649420bebbafa2c49dc56dcb (patch) | |
tree | 3a77f8a8bde046a8439b36feda370ac1655a20bb | |
parent | b95c5e0fae7be6efadd18f8ffe0b88bfd6d0313a (diff) |
default functions work
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | jfn.js | 12 |
2 files changed, 16 insertions, 1 deletions
@@ -12,6 +12,11 @@ jfn.defineFunction("testFunction", ["number"], function (num) { alert("Num: " + num); }); +jfn.defineFunction("testFunction", "*", function (data) { + alert("Catchall: " + JSON.stringify(data)); +}); + testFunction("Hello!"); //String: Hello! testFunction(1); //Num: 1 +testFunction({ "property": "value" }); //Catchall: {"property":"value"} ```
\ No newline at end of file @@ -20,6 +20,11 @@ jfn.getProxyFunction = function (name) { return; } } + + //No match found! + if (this[name]["jfn"].hasOwnProperty("default")) { + this[name]["jfn"]["default"].apply(this, arguments); + } }; }; @@ -37,7 +42,7 @@ jfn.defineFunction = function (object, name, args, fn) { if (!object[name].hasOwnProperty("__jfn")) { object[name]["__jfn"] = { "fns": [] }; - } else { + } else if (args !== "*") { for (var i = 0; i < object[name]["__jfn"]["fns"].length; i++) { var f = object[name]["__jfn"]["fns"][i]; if (JSON.stringify(f.args) === JSON.stringify(args)) { @@ -47,6 +52,11 @@ jfn.defineFunction = function (object, name, args, fn) { } } + if (args === "*") { + object[name]["__jfn"]["default"] = fn; + return; + } + object[name]["__jfn"]["fns"].push({ args: args, fn: fn |