{"id":294,"date":"2014-01-05T05:32:39","date_gmt":"2014-01-05T05:32:39","guid":{"rendered":"http:\/\/www.thespidercloud.com\/?p=91"},"modified":"2014-01-05T05:32:39","modified_gmt":"2014-01-05T05:32:39","slug":"temp","status":"publish","type":"post","link":"https:\/\/frowningbear.com\/codebase\/2014\/01\/05\/temp\/","title":{"rendered":"Passing Functions as Arguments part 1"},"content":{"rendered":"<p><!--more--><\/p>\n<pre class=\"lang:default decode:true\" title=\"Temp\">var doSomething = function(paramOne, paramTwo, fn) {\n  paramOne = paramOne * 8;\n  \/\/ return the result of calling this function\n  return fn(paramOne, paramTwo);\n};\n\nfunction sum(paramOne, paramTwo) {\n  \/\/ add the passed in arguments and return the value\n  return paramOne + paramTwo;\n}\n\n\/\/ set one\n\/\/ argument 3 is a reference to the predefined function\n\/\/ with the name \"sum\"\nvar foo = doSomething(2, 2, sum);\n\/\/ if that function didnt already exist we could define\n\/\/ and inject it directly into doSomething when we call it\n\/\/ So the beneath would be exactly equivalent because the \n\/\/ function you define on the spot is the same as the \n\/\/ predefined sum function\nvar bar = doSomething(2, 2, function(paramOne, paramTwo) {\n  return paramOne + paramTwo;\n});\n\/\/ and thats all we are doing in set two\n\n\/\/ if we consider primitive variables the concept is the same\n\/\/ we can have variable x containing value 5\nvar x = 5;\n\/\/ then alert it\nalert(x);\n\/\/ here variable x and its value are predined but if it wasnt\n\/\/ we could simply insert it into the alert function as we call it\nalert (5);\n\/\/ no need for a predefined variable just like no need for a\n\/\/ predefined function in set two \n\n\/\/ set two\nvar bar = doSomething(3, 4, function(paramOne, paramTwo) {\n  return paramOne * paramTwo;\n});<\/pre>\n<p><a title=\"Temp\" href=\"https:\/\/jsbin.com\/xiqote\/edit?js,console\" target=\"_blank\">Test code at jsbin<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":0,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"wp:attachment":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}