Created
November 2, 2011 12:32
-
-
Save abozhilov/1333507 to your computer and use it in GitHub Desktop.
Arguments default value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function func(a, f) { | |
return function (args) { | |
args.__proto__ = a; | |
f.call(this, args); | |
}; | |
}; | |
var f = func({foo : 10, bar : 20}, function (args) { | |
print(args.foo, args.bar); | |
}); | |
f({}); //10 20 | |
f({foo : 50}); //50 20 | |
f({foo : 50, bar : 50}); //50 50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ArtS, they are not interchangeable in this instance.
@abozhilov
In both my examples, you're only adding one line of code really. They're also very explicit about what they're doing.
I would argue that your function is less explicit and actually makes things much messier; by either creating a lot of noise at the point of function creation or clouding how the code works by decorating the function later on.
In the case of the former, you're now dealing with anonymous functions which make your stack traces less readable. Decorators are fine in principle, but in this case I just don't see the benefit. Obviously coders should use what they find most readable.
I do think this would be good for partial function application, or for modifying an existing function to take an object of arguments, etc.... I just don't think this should be used for giving functions default arguments and it definitely needs a more descriptive name.