{"id":201,"date":"2008-04-22T19:35:13","date_gmt":"2008-04-23T03:35:13","guid":{"rendered":"http:\/\/www.ericfeminella.com\/blog\/2008\/04\/22\/passing-rest-parameters-between-functions\/"},"modified":"2018-04-23T16:38:45","modified_gmt":"2018-04-23T20:38:45","slug":"passing-rest-parameters-between-functions","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2008\/04\/22\/passing-rest-parameters-between-functions\/","title":{"rendered":"Passing &#8230;(rest) parameters between functions"},"content":{"rendered":"<p>At some point when developing an application with ActionScript 3 you may need to pass a <a href=\"http:\/\/livedocs.adobe.com\/flex\/201\/html\/wwhelp\/wwhimpl\/common\/html\/wwhelp.htm?context=LiveDocs_Book_Parts&#038;file=03_Language_and_Syntax_160_19.html\" target=\"_blank\">&#8230;(rest) parameter <\/a> to a subsequent function call, and although at first this may appear to be pretty straightforward, doing so will not produce the results one might expect.<\/p>\n<p>For example, consider the following method <code>doSomething<\/code> which accepts a single &#8230;rest parameter:<\/p>\n<pre lang=\"actionscript\">\r\npublic function doSomething(...rest) : void {\r\n  var n:int = rest.length;\r\n  trace( \"...rest.length = \" + n );\r\n  for (var i:int = 0; i < n; i++) {\r\n    trace( rest[i] );\r\n  }\r\n}\r\ndoSomething(\"a\",\"b\",\"c\");\r\n\r\n\/\/ outputs:\r\n\/\/ ...rest.length = 3\r\n\/\/ a\r\n\/\/ b\r\n\/\/ c<\/pre>\n<p>Let's say we also have another function we need to invoke called <code>doSomethingElse<\/code> which accepts a ...(rest) parameter as well:<\/p>\n<pre lang=\"actionscript\">\r\npublic function doSomethingElse(...rest) : void {\r\n  var n:int = rest.length;\r\n  trace( \"...rest.length = \" + n );\r\n  for (var i:int = 0; i < n; i++) {\r\n    trace( rest[i] );\r\n  }\r\n}<\/pre>\n<p>Now suppose we need to pass the <code>...(rest)<\/code> parameter from the <code>doSomething<\/code> function to the <code>doSomethingElse<\/code> function. The result would be as follows:<\/p>\n<pre lang=\"actionscript\">\r\npublic function doSomething(...rest) : void {\r\n  var n:int = rest.length;\r\n  trace( \"...rest.length = \" + n );\r\n  for (var i:int = 0; i < n; i++) {\r\n    trace( rest[i] );\r\n  }\r\n  doSomethingElse( rest);\r\n}\r\n\/\/ doSomething outputs:\r\n\/\/ ...rest.length = 3\r\n\/\/ a\r\n\/\/ b\r\n\/\/ c\r\n\r\n\/\/ doSomethingElse outputs:\r\n\/\/ ...rest.length = 1\r\n\/\/ a,b,c<\/pre>\n<p>So what went wrong? What happens is the original ...rest parameter (passed to <code>doSomething<\/code>) is now being passed as a single parameter of type Array to the subsequent function (<code>doSomethingElse<\/code>), not as an Array of individual arguments as you may have expected.<\/p>\n<p>This can easily be resolved by using <a href=\"http:\/\/livedocs.adobe.com\/flex\/201\/langref\/Function.html#apply()\" target=\"_blank\">Function.apply();<\/a> as can be seen in the following:<\/p>\n<pre lang=\"actionscript\">\r\npublic function doSomething(...rest) : void {\r\n  var n:int = rest.length;\r\n  trace( \"...rest.length = \" + n );\r\n  for (var i:int = 0; i < n; i++) {\r\n    trace( rest[i] );\r\n  }\r\n  doSomethingElse.apply( null, rest);\r\n}\r\n\/\/ doSomething outputs:\r\n\/\/ ...rest.length = 3\r\n\/\/ a\r\n\/\/ b\r\n\/\/ c\r\n\r\n\/\/ doSomethingElse outputs:\r\n\/\/ ...rest.length = 3\r\n\/\/ a\r\n\/\/ b\r\n\/\/ c<\/pre>\n<p>What we are doing is applying the call to the <code>doSomethingElse<\/code> function with the original rest parameter as if we were invoking the function directly. So keep this in mind should you ever need to pass a ...rest parameter to a subsequent function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point when developing an application with ActionScript 3 you may need to pass a &#8230;(rest) parameter to a subsequent function call, and although at first this may appear to be pretty straightforward, doing so will not produce the results one might expect. For example, consider the following method doSomething which accepts a single &#8230;rest parameter: public function doSomething(&#8230;rest)&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2008\/04\/22\/passing-rest-parameters-between-functions\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[9],"tags":[],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-news"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}