<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: In javascript, how do you call a function? in Developers Questions</title>
    <link>https://community.esri.com/t5/developers-questions/in-javascript-how-do-you-call-a-function/m-p/1284331#M6572</link>
    <description>&lt;P&gt;When JS used Dojo (before 4.18 and 3.x), using l&lt;A href="https://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang-hitch" target="_self"&gt;ang.hitch&lt;/A&gt; was the proper way to make sure that "this" retains the proper context.&lt;/P&gt;&lt;P&gt;In ES6, &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" target="_self"&gt;arrow function&lt;/A&gt; were introduced where the "&lt;A href="http://es6-features.org/#Lexicalthis" target="_self"&gt;this&lt;/A&gt;" scope is better preserved.&lt;/P&gt;</description>
    <pubDate>Mon, 01 May 2023 14:28:58 GMT</pubDate>
    <dc:creator>KenBuja</dc:creator>
    <dc:date>2023-05-01T14:28:58Z</dc:date>
    <item>
      <title>In javascript, how do you call a function?</title>
      <link>https://community.esri.com/t5/developers-questions/in-javascript-how-do-you-call-a-function/m-p/1284132#M6571</link>
      <description>&lt;P&gt;I've written a constructor&amp;nbsp;function that registers an event handler:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;function MyConstructor(data, transport) {
this.data = data;
transport.on('data', function () {
alert(this.data);
});
}

// Mock transport object
var transport = {
on: function(event, callback) {
setTimeout(callback, 1200);
}
};

// called as
var obj = new MyConstructor('foo', transport);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;However, I am unable to access the created object's data property within the callback. It appears that this does not refer to the object that was created, but to another.&lt;/P&gt;
&lt;P&gt;I also attempted to use an object method rather than an anonymous function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;function MyConstructor(data, transport) {
this.data = data;
transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
alert(this.name);
};&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it has the same issues.&lt;/P&gt;
&lt;P&gt;How do I get to the right object?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 14:19:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/in-javascript-how-do-you-call-a-function/m-p/1284132#M6571</guid>
      <dc:creator>hectorsalamanca</dc:creator>
      <dc:date>2023-12-13T14:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: In javascript, how do you call a function?</title>
      <link>https://community.esri.com/t5/developers-questions/in-javascript-how-do-you-call-a-function/m-p/1284331#M6572</link>
      <description>&lt;P&gt;When JS used Dojo (before 4.18 and 3.x), using l&lt;A href="https://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang-hitch" target="_self"&gt;ang.hitch&lt;/A&gt; was the proper way to make sure that "this" retains the proper context.&lt;/P&gt;&lt;P&gt;In ES6, &lt;A href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" target="_self"&gt;arrow function&lt;/A&gt; were introduced where the "&lt;A href="http://es6-features.org/#Lexicalthis" target="_self"&gt;this&lt;/A&gt;" scope is better preserved.&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 14:28:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/in-javascript-how-do-you-call-a-function/m-p/1284331#M6572</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-05-01T14:28:58Z</dc:date>
    </item>
  </channel>
</rss>

