{"id":986,"date":"2022-08-30T15:10:29","date_gmt":"2022-08-30T15:10:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-closure-implicit-parameters-caller-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:29","modified_gmt":"2022-08-30T15:10:29","slug":"javascript-closure-implicit-parameters-caller-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-closure-implicit-parameters-caller-collection-of-common-programming-errors\/","title":{"rendered":"javascript closure: implicit parameters, caller-Collection of common programming errors"},"content":{"rendered":"<p>I need some clarification with javascript call&#8217;s metadata. Provided code below has SenderAsParameterFunc and NoExplicitParametersFunc which are being called from AnotherFunction&#8217;s each loops.<\/p>\n<pre><code>var Obj = function(){\n\nObj.SenderAsParameterFunc = function(sender, param1, param2){\n\/\/ log $(sender).id;\n}\n\nObj.NoExplicitParametersFunc = function(){\n\n \/\/ EXTRACT DEFAULT SENDER\/CALLEE\n \/\/ I see in console Obj.NoExplicitParametersFunc.caller.arguments &gt;&gt; SEE REF1\n}\n\n Obj.NoExplicitParametersFuncWithExtraArgs = function(this, par1, par2){\n \/\/ 'this' from each loop?\n}\n\nObj.AnotherFunc = function(){\n\n    var X = $('myselect');\n\n    var par1 = 1;\n    var par2 = 'bla';\n    $.each(X.find(\"option\"), function () {\n                Obj.SenderAsParameterFunc(this, par1, par2); \n                \/\/ 'this' here references current object from 'each' loop\n            });\n\n    $.each(X.find(\"option\"), Obj.NoExplicitParametersFunc); \n   \/\/ must be equvalent to first var, \n   \/\/ but to extract 'inner' this we have to do more magic in NoExplicitParametersFunc...\n\n    var par1 = 1;\n    var par2 = 'bla';\n    $.each(X.find(\"option\"), Obj.NoExplicitParametersFuncWithExtraArgs);\n    }\n\n}\n<\/code><\/pre>\n<p>So WHAT should be written in NoExplicitParametersFunc to get access to each option as that is done in SenderAsParameterFunc with $(this)?<\/p>\n<p>==========<\/p>\n<p>OK, TO Summarize what I&#8217;ve learned: if a function goes without parameters<\/p>\n<pre><code>$.each(X.find(\"option\"), Obj.NoExplicitParametersFunc);\n<\/code><\/pre>\n<p>then &#8216;this&#8217; is passed by default and is accessible within a function.<\/p>\n<p>If a function has parameters<\/p>\n<pre><code>$.each(X.find(\"option\"), function () { Obj.SenderAsParameterFunc(this, par1, par2); });\n<\/code><\/pre>\n<p>then &#8216;this&#8217; MUST be included as a parameter (first?) to be accessible within the function.<\/p>\n<p>=============<\/p>\n<p>REF1:<\/p>\n<pre><code>b.Event\naltKey: undefined\nattrChange: undefined\nattrName: undefined\nbubbles: true\ncancelable: false\nctrlKey: undefined\ncurrentTarget: input#inputId\ndata: undefined\ndelegateTarget: input#inputId\neventPhase: 2\nhandleObj: Object\nisDefaultPrevented: function ot(){return!1}\njQuery19105656534675508738: true\nmetaKey: false\noriginalEvent: Event\nrelatedNode: undefined\nrelatedTarget: undefined\nshiftKey: undefined\nsrcElement: input#inputId\ntarget: input#inputId\ntimeStamp: 1366622725473\ntype: \"change\"\nview: undefined\nwhich: undefined\n__proto__: Object,\n<\/code><\/pre>\n<p>currentTarget, delegateTarget, srcElement, target ARE same, but what should be actually used, the most correct way? References would be appreciated!<\/p>\n<p>Thanks.<\/p>\n<ol>\n<li>\n<p><strong>TL;DR:<\/strong> In your case it doesn&#8217;t matter, but if you are in doubt use <code>currentTarget<\/code><\/p>\n<p>I don&#8217;t really understand your first question, but about the second one&#8230;<\/p>\n<p>When an event is dispatched usually this event bubbles, for example on this page:<\/p>\n<pre><code>\n  \n    <img decoding=\"async\" src=\"foobar.png\" \/>\n  \n\n<\/code><\/pre>\n<p>If the user clicks a <code>img<\/code> element inside a <code>div<\/code> it will fire the events this way<\/p>\n<pre><code>body - capturing\ndiv - capturing\nimg - at target\ndiv - bubbling\nbody - bubbling\n<\/code><\/pre>\n<p>So you can add a listener to <code>body<\/code> and you&#8217;ll catch the event. But to allow you to know which element was ACTUALLY clicked (the <code>img<\/code> in this case) it provides <code>event.target<\/code> property (<code>srcElement<\/code> on Internet Explorer).<\/p>\n<p><code>delegateTarget<\/code> is a property added by jQuery and it provides extra info if you use event delegation. I don&#8217;t know exactly what it is but you can take a look at the documentation<\/p>\n<p>And finally <code>currentTarget<\/code> is the element you attached the event, if we added the listener to <code>body<\/code> it&#8217;ll be <code>body<\/code>. So the arguments on each events are<\/p>\n<pre><code>fire body.click with { target: img, currentTarget: body }\nfire div.click  with { target: img, currentTarget: div  }\nfire img.click  with { target: img, currentTarget: img  }\nfire div.click  with { target: img, currentTarget: div  }\nfire body.click with { target: img, currentTarget: body }\n<\/code><\/pre>\n<p>Depending on what you are doing you will need one or the other property.<\/p>\n<\/li>\n<li>\n<blockquote>\n<pre><code>$.each(X.find(\"option\"), function () {\n      Obj.SenderAsParameterFunc(this);\n      \/\/ 'this' here references anonymous function, \n      \/\/ from which it was called, so 'this' is sender\n});\n<\/code><\/pre>\n<\/blockquote>\n<p>No. The <em>anonymous<\/em> function is not referenced by anything here (it is accessible as a parameter only in <code>$.each<\/code>). The <code>this<\/code> keyword in the callbacks of <code>$.each<\/code> does reference the iterated items. That also does answer your question:<\/p>\n<pre><code>Obj.NoExplicitParametersFunc = function(i, v){\n    console.log(this, i, v);\n}\n<\/code><\/pre>\n<blockquote>\n<p>If a function has parameters [and is called in an anonymous function expression] then &#8216;this&#8217; MUST be included as a parameter (first?) to be accessible within the function.<\/p>\n<\/blockquote>\n<p>Yes, as <code>this<\/code> is unique to each function call you have to pass its value along &#8211; just like you cannot just access local variables of your caller function, you cannot access its <code>this<\/code> value. To call the function with a specific this-value, you can use <code>.call<\/code>:<\/p>\n<pre><code>$.each(X.find(\"option\"), function(i, v) {\n    Obj.SenderAsParameterFunc.call(this, i, v, par1, par2);\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:11:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I need some clarification with javascript call&#8217;s metadata. Provided code below has SenderAsParameterFunc and NoExplicitParametersFunc which are being called from AnotherFunction&#8217;s each loops. var Obj = function(){ Obj.SenderAsParameterFunc = function(sender, param1, param2){ \/\/ log $(sender).id; } Obj.NoExplicitParametersFunc = function(){ \/\/ EXTRACT DEFAULT SENDER\/CALLEE \/\/ I see in console Obj.NoExplicitParametersFunc.caller.arguments &gt;&gt; SEE REF1 } Obj.NoExplicitParametersFuncWithExtraArgs = [&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-986","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/986","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=986"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/986\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}