Uploaded image for project: 'Batik'
  1. Batik
  2. BATIK-321

"this" value is not always available in EventListener

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Resolution: Incomplete
    • 1.5
    • None
    • Scripting
    • None
    • Operating System: Windows XP
      Platform: PC

    Description

      I was trying to use the following ECMAScript with Batik 1.5 beta 4's Squiggle,
      but I noticed some inconsistent behavior:

      ========= START CODE =========
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE svg PUBLIC "//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC
      SVG-20010904/DTD/svg10.dtd">
      <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" id="testSVG">
      <script type="text/ecmascript"><![CDATA[
      function TestObj()
      {
      this.root = document.documentElement;
      this.abc = 123;
      this.myFunc = function()

      { alert(this.abc); }

      this.root.addEventListener("click", this.myFunc, false);
      }
      var obj = new TestObj();
      ]]></script>
      <rect width="100%" height="100%" fill="green"/>
      <rect x="45%" y="45%" width="10%" height="10%" onmouseover="obj.myFunc()"/>
      </svg>
      ========= END CODE =========

      When addEventListener() is explicitly called to add the TestObj.myFunc method
      as an event listener, and myFunc gets called as the result of an event,
      the "instance variable" this.abc is undefined. However, if I add the same
      function as an event listener via an onmouseover attribute, that instance
      variable is defined. This inconsistent behavior exists Adobe's viewer as well.

      To see the undefined variable behavior, click anywhere in the SVG window. To
      see the "proper" behavior, mouse over the black rectangle.

      It seems to me the click behavior should be the same as the mouseover behavior.

      Probably, addEventListener() is treating its function argument as a simple
      function instaed of an object method.

      Attachments

        Activity

          People

            batik-dev@xmlgraphics.apache.org Batik Developer's Mailing list
            cybersam@alum.rpi.edu Samuel C. Yang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: