Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Passing 'this' to an anonymous function in an event listener

    Hello!

    I'm working with nested functions and trying to pass a 'this' value to an anonymous being used in an assignment for an event listener. Here's the basics of my code:

    Code:
    <div id='abc'></div>
    <script type='text/javascript'>
    var abc = function () {
        this.myFunction = function() {
            var myObj
            myObj = document.createElement("input");
            myObj.setAttribute("type", "button");
            myObj.setAttribute("value", "Click Me");
            myObj.addEventListener("click", function () {
                this.doDing();
            }, false);
            document.getElementById('abc').appendChild(myObj);
        }
        this.doDing = function () { alert('ding'); }
    }
    var myInstance = new abc();
    myInstance.myFunction();
    </script>
    So, this should plop a button inside our DIV and when clicked I'd like it to run the alert-ding; unfortunately it seems to want to run the function as defined under the buttons object which doesn't work out too well.

    Any suggestions?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    I think you'd have to do it like this:

    Code:
    var abc = function () {
        this.myFunction = function() {
            var myObj
            var abcinst=this;
            myObj = document.createElement("input");
            myObj.setAttribute("type", "button");
            myObj.setAttribute("value", "Click Me");
            myObj.addEventListener("click", function () {
                abcinst.doDing();
            }, false);
            document.getElementById('abc').appendChild(myObj);
        }
        this.doDing = function () { alert('ding'); }
    }
    var myInstance = new abc();
    myInstance.myFunction();

  • Users who have thanked gusblake for this post:

    Malcolm (06-10-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yep, that works. I was hoping to do the something that wouldn't make another copy of the object in memory.

    Thanks!


    Edit:
    Oh, I lie, this creates a reference to the object and uses no additional memory.
    Last edited by Malcolm; 06-10-2010 at 09:35 PM. Reason: Reading about JS memory usage filled in the blanks

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    there’s an even easier solution for your case.
    PHP Code:
    var abc = function () {
        
    this.myFunction = function() {
            var 
    myObj
            myObj 
    document.createElement("input");
            
    myObj.setAttribute("type""button");
            
    myObj.setAttribute("value""Click Me");
            
    myObj.addEventListener("click"this.doDingfalse);
            
    document.getElementById('abc').appendChild(myObj);
        }
        
    this.doDing = function () { alert('ding'); }
    }
    var 
    myInstance = new abc();
    myInstance.myFunction(); 
    it works because there’s no this in .doDing()
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •