From 90c9c69ff0b84633674f7f32f0abc141da0820fd Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Mon, 30 Jul 2007 04:38:20 +0000 Subject: [PATCH] prototype: Add Function#argumentNames, which returns an ordered array of the function's named arguments. --- CHANGELOG | 2 ++ src/base.js | 5 +++++ test/unit/base.html | 7 +++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 43db9e1..00ed9d3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add Function#argumentNames, which returns an ordered array of the function's named arguments. [sam] + * Add Prototype.Browser.MobileSafari which evaluates to true on the iPhone's browser. [sam] * Optimize Selector#match and Element#match for simple selectors. Closes #9082. [Andrew Dupont] diff --git a/src/base.js b/src/base.js index 143aa4c..c89aa55 100644 --- a/src/base.js +++ b/src/base.js @@ -79,6 +79,11 @@ Object.extend(Object, { }); Object.extend(Function.prototype, { + argumentNames: function() { + var names = this.toString().match(/^function\s*\((.*?)\)/)[1].split(",").invoke("strip"); + return names.length == 1 && !names[0] ? [] : names; + }, + bind: function() { if (arguments.length < 2 && arguments[0] === undefined) return this; var __method = this, args = $A(arguments), object = args.shift(); diff --git a/test/unit/base.html b/test/unit/base.html index fb0b210..97fe526 100644 --- a/test/unit/base.html +++ b/test/unit/base.html @@ -56,6 +56,13 @@ new Test.Unit.Runner({ + testFunctionArgumentNames: function() { with(this) { + assertEnumEqual([], (function() {}).argumentNames()); + assertEnumEqual(["one"], (function(one) {}).argumentNames()); + assertEnumEqual(["one", "two", "three"], (function(one, two, three) {}).argumentNames()); + assertEqual("$super", (function($super) {}).argumentNames().first()); + }}, + testFunctionBind: function() { with(this) { function methodWithoutArguments() { return this.hi }; function methodWithArguments() { return this.hi + ',' + $A(arguments).join(',') };