How to refer to current object

Discussion in 'Javascript' started by Kai Zhu, Oct 29, 2006.

  1. Kai Zhu

    Kai Zhu Guest

    I've write a script like this.
    <script>
    function A() {
    this.onMouseDown = function(event) {
    .....
    }
    }
    var objA = new A();
    htmlelement.onmousedown = a.onmousedown;
    </script>

    But in the function onMouseDown, I want to refer to the object A itself. But
    when I try "this" keyword,
    I only get the htmlelement, but not objA. How can I refer to objA in the
    onMouseDown function?
    Kai Zhu, Oct 29, 2006
    #1
    1. Advertising

  2. Kai Zhu wrote:
    > I've write a script like this.
    > <script>
    > function A() {
    > this.onMouseDown = function(event) {
    > .....
    > }
    > }
    > var objA = new A();
    > htmlelement.onmousedown = a.onmousedown;
    > </script>
    >
    > But in the function onMouseDown, I want to refer to the object A itself. But
    > when I try "this" keyword,
    > I only get the htmlelement, but not objA. How can I refer to objA in the
    > onMouseDown function?


    htmlelement.onmousedown = function(){A.call(htmlelement)};

    Or, using the YUI toolkit ( http://developer.yahoo.com/yui/ )

    YAHOO.util.Dom.addListener(htmlelement, "mousedown", A, true);

    Note, this allows you to *add* listeners, not overwrite one handler.

    Nige
    TheBagbournes, Oct 29, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    251
  2. Mike
    Replies:
    4
    Views:
    514
    Stargaming
    Jan 7, 2008
  3. Aaron Fude
    Replies:
    3
    Views:
    495
    Roedy Green
    Jun 11, 2008
  4. Refer to current DIV tag

    , Dec 19, 2007, in forum: Javascript
    Replies:
    2
    Views:
    86
  5. laredotornado

    How to refer to the current Javascript file

    laredotornado, Sep 16, 2008, in forum: Javascript
    Replies:
    4
    Views:
    90
    dhtml
    Sep 21, 2008
Loading...

Share This Page