{"id":987,"date":"2017-10-22T13:25:31","date_gmt":"2017-10-22T13:25:31","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=987"},"modified":"2024-12-08T20:46:44","modified_gmt":"2024-12-08T20:46:44","slug":"get-function-name-in-strict-mode","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=987","title":{"rendered":"get function name in strict mode"},"content":{"rendered":"<blockquote><p>Goal: show the name of the called function in strict mode<\/p><\/blockquote>\n<p>If I want to see the function name I can put it like this<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n'use strict';\r\nvar myFunction = function(){\r\n  \/\/ ...\r\n  console.log('myFunction called');\r\n  \/\/ ...\r\n}\r\n<\/pre>\n<p>but I like the globalized solutions and don&#8217;t forget the strict mode, where the possibilities are restricted.<br \/>\nThere is nothing else to do just throw an exception and read from the error stack like this:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n'use strict';\r\nvar myFunction = function(){\r\n  \/\/ ...\r\n  try {\r\n    throw new Error();\r\n  }\r\n  catch (e) {\r\n    console.log('Function called:', e.stack.split('at ')[1].split(' ')[0].split('.').last());\r\n  }\r\n  \/\/ ...\r\n}\r\n<\/pre>\n<p>in globalized version<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n'use strict';\r\nvar showMyName = function(){\r\n  try {\r\n    throw new Error();\r\n  }\r\n  catch (e) {\r\n    console.log('Function called:', e.stack.split('at ')[2].split(' ')[0].split('.').last());\r\n  }\r\n};\r\n\r\nvar myFunction = function(){\r\n  \/\/ ...\r\n  showMyName();\r\n  \/\/ ...\r\n};\r\n\r\nmyFunction();\r\n\r\n<\/pre>\n<p>Be careful in the second version the last called function is the showMyName() itself that is why we need the previous one from the stack \ud83d\ude42<\/p>\n<p>One more thing: the end of the long split row &#8230; <code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">e.stack.split('at ')[2].split(' ')[0].split('.').last()<\/code><br \/>\nthe <code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">split()<\/code> result is an array and we need the last element &#8211; so the last() is an Array object extend like this<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nArray.prototype.last=function(){\r\n  return this[this.length - 1];\r\n};\r\n<\/pre>\n<p>so the picture is complete now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: show the name of the called function in strict mode If I want to see the function name I can put it like this but I like the globalized solutions and don&#8217;t forget the strict mode, where the possibilities are restricted. There is nothing else to do just throw an exception and read from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-javascript","category-tricky"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=987"}],"version-history":[{"count":9,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":1189,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/987\/revisions\/1189"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}