summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatthewsotoudeh <matthewsot@outlook.com>2015-05-10 17:49:29 -0700
committermatthewsotoudeh <matthewsot@outlook.com>2015-05-10 17:49:29 -0700
commitb5a7c325aa6102fc649420bebbafa2c49dc56dcb (patch)
tree3a77f8a8bde046a8439b36feda370ac1655a20bb
parentb95c5e0fae7be6efadd18f8ffe0b88bfd6d0313a (diff)
default functions work
-rw-r--r--README.md5
-rw-r--r--jfn.js12
2 files changed, 16 insertions, 1 deletions
diff --git a/README.md b/README.md
index c72c0ce..0f455f6 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/jfn.js b/jfn.js
index c8b6534..25998dc 100644
--- a/jfn.js
+++ b/jfn.js
@@ -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
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback