{"id":3790,"date":"2020-06-05T07:22:47","date_gmt":"2020-06-05T07:22:47","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3790"},"modified":"2020-06-05T07:43:52","modified_gmt":"2020-06-05T07:43:52","slug":"javascript-function-basics","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/javascript-function-basics\/","title":{"rendered":"Javascript Function Basics"},"content":{"rendered":"<ol>\n<li>Function is represented as Object in Javascript<\/li>\n<li>Has 2 phases &#8211; Function Definition and Function Execution<\/li>\n<li>Two ways of defining function<br \/>\n<strong>Function Declaration \/ Named Function<\/strong>  &#8211; Function object would get created at scope creation phase<br \/>\n<strong>Function Expression \/ Anonymous Function <\/strong>&#8211; Function object would get created at execution phase &#8211; Interepreter would throw error incase the function is called before anonymous definition.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">  \r\n  \/\/Named Function\r\n displayAge(); \r\n \r\n function displayAge(){\r\n  console.log('My Age is 33')\r\n } \r\n \r\n \/\/Anonymous Function \r\n var age = function(){ \/\/Context\/scope execution phase\r\n   console.log('My Age is 33')\r\n } \r\n age();\r\n<\/pre>\n<\/li>\n<li>No concept of function overloading. Function with near matching argument would be called.In the below code getSum has 2 arguments but still it gets called.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction getSum(num1, num2) {\r\n  console.log('Function overloading is not possible');\r\n}\t\r\ngetSum();\r\n<\/pre>\n<pre>\r\n  Function overloading is not possible\r\n<\/pre>\n<\/li>\n<li>Function namespace context would be created with the samename as the function namespace<\/li>\n<li>In the below code the getLunch appears to be overloaded but there would be only one namespace in context with name getLunch<\/li>\n<li>So you may expect the output to be different but all the times getLunch(buffey, paid)  would be called in below code\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction getLunch() {\r\n  console.log('Free Lunch');\r\n}\r\n\r\nfunction getLunch(paidLunch) {\r\n  console.log('paidLunch');\r\n}\r\n\r\nfunction getLunch(buffey, paid) {\r\n  console.log('paidLunch buffey');\r\n}\r\ngetLunch();\r\ngetLunch(5);\r\ngetLunch(5,10);\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\npaidLunch buffey\r\npaidLunch buffey\r\npaidLunch buffey\r\n<\/pre>\n<\/li>\n<li>So what would be the workaround. Check the code as below\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\"> \r\n  function getLunch() {\r\n  if(arguments.length === 0)\r\n    console.log('Free Lunch');\r\n  \r\n  if(arguments.length === 1)\r\n    console.log('paidLunch');\r\n    \r\n  if(arguments.length === 2)\r\n    console.log('paidLunch buffey');\r\n  }\r\n  \r\n   getLunch();\r\n   getLunch(5);\r\n   getLunch(5,10);\r\n<\/pre>\n<p>  <strong>Output<\/strong><\/p>\n<pre>\r\nFree Lunch\r\npaidLunch\r\npaidLunch buffey\r\n<\/pre>\n<\/li>\n<li>Using Restparameter feature from ECMAScript6\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\"> \r\nfunction getLunch(bill, space, ...menu) {\r\n  console.log(bill);\r\n  console.log(space);\r\n  console.log(menu);\r\n}\r\n\r\ngetLunch(150, 'Open Terrace', 'idly', 'dosa', 'vada');\r\n<\/pre>\n<p>\t<strong>Output<\/strong><\/p>\n<pre>\r\n150\r\nOpen Terrace\r\n[\"idly\", \"dosa\", \"vada\"]\r\n<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Function is represented as Object in Javascript Has 2 phases &#8211; Function Definition and Function Execution Two ways of defining function Function Declaration \/ Named Function &#8211; Function object would get created at scope creation phase Function Expression \/ Anonymous Function &#8211; Function object would get created at execution phase &#8211; Interepreter would throw error&hellip; <a href=\"https:\/\/codethataint.com\/blog\/javascript-function-basics\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[],"class_list":["post-3790","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3790"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3790\/revisions"}],"predecessor-version":[{"id":3800,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3790\/revisions\/3800"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}