{"id":774,"date":"2022-08-30T15:06:57","date_gmt":"2022-08-30T15:06:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/call-apply-method-with-arguments-in-javascript-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:57","modified_gmt":"2022-08-30T15:06:57","slug":"call-apply-method-with-arguments-in-javascript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/call-apply-method-with-arguments-in-javascript-collection-of-common-programming-errors\/","title":{"rendered":"Call apply method with arguments in JavaScript-Collection of common programming errors"},"content":{"rendered":"<p>First notice that you are actually invoking your function immediately, the line:<\/p>\n<pre><code>test(this).apply(body);\n<\/code><\/pre>\n<p>Fails because at first, you call the <code>test<\/code> function passing <code>this<\/code> <em>as an argument<\/em>, and then you try to access an <code>apply<\/code> property on the function&#8217;s result, which is <code>undefined<\/code> since your function doesn&#8217;t return anything.<\/p>\n<p>That property access on the <code>undefined<\/code> value is giving you the <code>TypeError<\/code> exception.<\/p>\n<p>Now, let&#8217;s see what you actually want to do, if you want to pass the value of the <code>body<\/code> variable as the argument of your function, setting the outer <code>this<\/code> value, as the <code>this<\/code> value of <code>test<\/code>, you can do:<\/p>\n<pre><code>test.apply(this, [body]);\n<\/code><\/pre>\n<p>But that&#8217;s why the <code>call<\/code> method exists, when you know exactly which arguments to pass, you don&#8217;t need to create an array for nothing, you just pass the arguments:<\/p>\n<pre><code>test.call(this, body);\n<\/code><\/pre>\n<p>The <code>apply<\/code> method is really useful when you deal with for example a dynamic number of arguments, when you know which arguments want to pass, and still have the ability of setting the <code>this<\/code> value, <code>call<\/code> is just what you need.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:41:48. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>First notice that you are actually invoking your function immediately, the line: test(this).apply(body); Fails because at first, you call the test function passing this as an argument, and then you try to access an apply property on the function&#8217;s result, which is undefined since your function doesn&#8217;t return anything. That property access on the undefined [&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-774","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/774","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=774"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/774\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}