{"id":2309,"date":"2022-08-30T15:23:47","date_gmt":"2022-08-30T15:23:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/detecting-console-log-calls-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:47","modified_gmt":"2022-08-30T15:23:47","slug":"detecting-console-log-calls-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/detecting-console-log-calls-collection-of-common-programming-errors\/","title":{"rendered":"Detecting console.log() calls-Collection of common programming errors"},"content":{"rendered":"<p>You can replace <code>console.log<\/code> with your own function, which determines if it&#8217;s been called correctly. Here&#8217;s a example of how you could confirm that a function logs a certain message:<\/p>\n<pre><code>function doesLogMessage(f, message) {\n    var oldLog = console.log,\n        result = false;\n\n    console.log = function(s) {\n        if (s == message) {\n            result = true;\n        }\n    };\n\n    f();\n\n    console.log = oldLog;\n\n    return result;\n}\n<\/code><\/pre>\n<h3>Example Usage<\/h3>\n<pre><code>function exampleFunction() {\n    console.log(\"Hello World\");\n}\n\ndoesLogMessage(exampleFunction, \"Hello World\"); \/\/ true\n<\/code><\/pre>\n<pre><code>function exampleFunction2() {\n    console.log(\"Preparing world...\");\n    console.log(\"Hello World\");\n}\n\ndoesLogMessage(exampleFunction2, \"Hello World\"); \/\/ true\n<\/code><\/pre>\n<pre><code>function exampleFunction3() { }\n\ndoesLogMessage(exampleFunction3, \"Hello World\"); \/\/ false\n<\/code><\/pre>\n<pre><code>function exampleFunction4() {\n    console.log(\"Goodbye World\");\n}\n\ndoesLogMessage(exampleFunction4, \"Hello World\"); \/\/ false\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-01-05 10:08:25. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>You can replace console.log with your own function, which determines if it&#8217;s been called correctly. Here&#8217;s a example of how you could confirm that a function logs a certain message: function doesLogMessage(f, message) { var oldLog = console.log, result = false; console.log = function(s) { if (s == message) { result = true; } }; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2309","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2309"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2309\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}