Wes Gamble
2006-Jul-13 17:24 UTC
[Rails] Does Javascript let you default input params like Ruby does?
Can you have a Javascript function with a param that''s defaulted if it isn''t present? function X(a, b = false, c = ''dog'') { } X(5) calls X with a=5, b=false, c=''dog'' X(4, true) calls X with a=4, b=true, c=''dog'' X(3, true, ''cat'') calls X with a=3, b=true, c=''cat'' ? -- Posted via http://www.ruby-forum.com/.
Mark Van Holstyn
2006-Jul-13 19:17 UTC
[Rails] Does Javascript let you default input params like Ruby does?
nope. On 7/13/06, Wes Gamble <weyus@att.net> wrote:> > Can you have a Javascript function with a param that''s defaulted if it > isn''t present? > > function X(a, b = false, c = ''dog'') { > } > > X(5) calls X with a=5, b=false, c=''dog'' > X(4, true) calls X with a=4, b=true, c=''dog'' > X(3, true, ''cat'') calls X with a=3, b=true, c=''cat'' > > ? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/a66da002/attachment.html
Wes Gamble
2006-Jul-13 19:37 UTC
[Rails] Re: Does Javascript let you default input params like Ruby d
robert bradford wrote:> I hope I am wrong, but I believe I remember hearing that this cannot be > done like it is in RoR, PHP, etc. I remember seeing something like this: > > <script type="text/javascript"> > function hello(a) { > if (typeof a==''undefined'') a = "world"; //Do this for each param you > would like to default > > alert(''Hello, '' + a); > } > </script> > > <%= button_to_function "Greet the world", "hello()" %> > <%= button_to_function "General Greeting", "hello(''there'')" %> > > This is kind of a lame work around, but it works. Hopefully there is a > better solution.I probably shouldn''t have asked - I found out that you _can_ do this but not defaulting in the function definition. You have to interrogate the built-in Javascript "arguments" array to see if there are extra arguments and then default them based on your needs, etc. There is no explicit contract between the caller and the callee - I can pass as many arguments as I want to a Javascript function and the number of arguments that it receives can be less. In this way, Javascript is even more dynamic than Ruby in it''s calling semantics since Ruby will throw a run time error if you call a function with the wrong number of parameters - as I''m sure all of us Railists know :)! Wes -- Posted via http://www.ruby-forum.com/.
robert bradford
2006-Jul-13 19:56 UTC
[Rails] Re: Does Javascript let you default input params like Ruby d
I hope I am wrong, but I believe I remember hearing that this cannot be done like it is in RoR, PHP, etc. I remember seeing something like this: <script type="text/javascript"> function hello(a) { if (typeof a==''undefined'') a = "world"; //Do this for each param you would like to default alert(''Hello, '' + a); } </script> <%= button_to_function "Greet the world", "hello()" %> <%= button_to_function "General Greeting", "hello(''there'')" %> This is kind of a lame work around, but it works. Hopefully there is a better solution. -- Posted via http://www.ruby-forum.com/.
Ben Bleything
2006-Jul-13 19:56 UTC
[Rails] Does Javascript let you default input params like Ruby does?
On Thu, Jul 13, 2006, Wes Gamble wrote:> Can you have a Javascript function with a param that''s defaulted if it > isn''t present? > > function X(a, b = false, c = ''dog'') { > } > > X(5) calls X with a=5, b=false, c=''dog'' > X(4, true) calls X with a=4, b=true, c=''dog'' > X(3, true, ''cat'') calls X with a=3, b=true, c=''cat''The syntax is not so clean, but I ran across this the other day which looks like what you want: http://earthcode.com/blog/2006/01/optional_args.html Ben
Eric Anderson
2006-Jul-13 20:06 UTC
[Rails] Re: Does Javascript let you default input params like Ruby does?
Wes Gamble wrote:> Can you have a Javascript function with a param that''s defaulted if it > isn''t present? > > function X(a, b = false, c = ''dog'') { > } > > X(5) calls X with a=5, b=false, c=''dog'' > X(4, true) calls X with a=4, b=true, c=''dog'' > X(3, true, ''cat'') calls X with a=3, b=true, c=''cat'' > > ?There is no language support for it but there are ways to emulate it since Javascript does not require you to send all arguments. Eg. function X(a, b, c) { if( arguments.length < 3 ) c = ''dog'' if( arguments.length == 1 ) b = false //... implementation } Some people will use the short-circuited "or" operator as follows: function Y(m, n, o) { n = n || ''foo'' o = o || ''bar'' //... implementation } This can be dangerous if a valid value for "n" or "o" can evaluate to false. In Ruby only "nil" and "false" evaluate to false but in Javascript the follow all evaluate to false: undefined null 0 -0 "" (empty string) NaN An empty string and 0 are often valid values. Prototype has this fault in it''s implementation of the $F() function for <select> input elements. Another entirely different option is to use object literals to emulate optional named arguments. So: function M(name, options) { defaults = { foo: ''bar'', baz: ''cat'' } options.prototype = defaults //... implementation using "options" to access //... optional arguments } You would call this like: M(''some_val'', { foo: ''dog'', baz: ''car'' }); If you remove an option it would get the default specified. So: M(''another_val'', { foo: ''dog'' }); Eric
Possibly Parallel Threads
- Using an image button for "link_to_remote"
- Hard time understanding the differences between "def self.foo" and "def foo"
- Using <%= text_field %> within partials is problematic
- Implementing HTTPS with WEBrick?
- Setup new data in the test database _after_ unit test runs