prototype: Add Function#argumentNames, which returns an ordered array of the function's named arguments.

This commit is contained in:
Sam Stephenson 2007-07-30 04:38:20 +00:00
parent 5645a0783f
commit 90c9c69ff0
3 changed files with 14 additions and 0 deletions

View File

@ -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]

View File

@ -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();

View File

@ -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(',') };