{"id":2793,"date":"2011-06-30T22:27:46","date_gmt":"2011-06-30T19:27:46","guid":{"rendered":"http:\/\/mummila.net\/nuudelisoppa\/?p=2793"},"modified":"2011-06-30T22:27:46","modified_gmt":"2011-06-30T19:27:46","slug":"jquery-style-function-calls-with-callback-parameters-parameter-expansion-defaults","status":"publish","type":"post","link":"https:\/\/mummila.net\/nuudelisoppa\/2011\/06\/30\/jquery-style-function-calls-with-callback-parameters-parameter-expansion-defaults\/","title":{"rendered":"jQuery-style function calls with callback parameters &amp; parameter expansion defaults"},"content":{"rendered":"<p><strong>Disclaimers:<\/strong><\/p>\n<ol>\n<li>I&#8217;m still learning my way with jQuery.<\/li>\n<li>Code here is pseudocode carved from functioning code (and even then only partly so), so it&#8217;s untested in real world.<\/li>\n<\/ol>\n<p>I was curious as to how jQuery does parameter expansion in event handlers with callbacks. Googling I could only come up with <a href=\"http:\/\/enterprisejquery.com\/2010\/08\/javascript-parameter-patterns-with-extend-and-beyond\/\" title=\"Enterprise jQuery: Javascript Parameter Patterns with $.extend and Beyond\">resources that would call their custom functions with parameters given as object literals<\/a>, like so:<\/p>\n<pre><code>myFunc({\r\n  parameter1 : 12345,\r\n  parameter2 : \"foo\"\r\n});<\/code><\/pre>\n<p>, which, when using a callback parameter, would go something like:<\/p>\n<pre><code>myFunc({\r\n  parameter1 : 12345,\r\n  callback : function() {\r\n    return true;\r\n  }\r\n});<\/code><\/pre>\n<p>Now, there&#8217;s nothing wrong with that as such. What <em>I<\/em> wanted however, was to be able to call my custom functions in the elegant way jQuery does it, <em>without<\/em> the object literal brackets surrounding the parameters, like so:<\/p>\n<pre><code>myFunc(12345, function() {\r\n    return true;\r\n});<\/code><\/pre>\n<p>Here&#8217;s how I could achieve this:<\/p>\n<pre><code>jQuery.fn.myFunc = function() {\r\n\tvar options = {\r\n\t\tparameter1 : (arguments[0] || 0),\r\n\t\tcallback : (arguments[1] || function() { return false; })\r\n\t}\r\n\r\n\tdo_stuff_with_param1(options.parameter1);\r\n        return options.callback();\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Disclaimers: I&#8217;m still learning my way with jQuery. Code here is pseudocode carved from functioning code (and even then only partly so), so it&#8217;s untested in real world. I was curious as to how jQuery does parameter expansion in event handlers with callbacks. Googling I could only come up with resources that would call their [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2793","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts\/2793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/comments?post=2793"}],"version-history":[{"count":19,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts\/2793\/revisions"}],"predecessor-version":[{"id":2812,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/posts\/2793\/revisions\/2812"}],"wp:attachment":[{"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/media?parent=2793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/categories?post=2793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mummila.net\/nuudelisoppa\/wp-json\/wp\/v2\/tags?post=2793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}