need more understanding of how to use the word "this"

Discussion in 'Javascript' started by lawrence, Nov 6, 2004.

  1. lawrence

    lawrence Guest

    I'm trying to gain a better understanding of javascript by studying
    examples. I noticed this in an online tutorial. I don't get the use of
    "this".

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    You might want to add some event handler for an event to an element to
    which there is already an event handler attached. That is possible by
    storing the old event handler somewhere and then writing a new
    function
    which calls the old event handler.
    For example suppose you have a document with buttons with onclick
    handlers
    <INPUT TYPE="button" NAME="aButton" VALUE="praise God"
    ONCLICK="alert('Hail the Kibo');"
    >

    and you want add script that disables the event handler temporarily
    then
    you store the old event handler
    var button = document.formName.aButton;
    button.oldOnClick = button.onclick;
    and write your new click handler which calls the oldOnClick when
    necessary
    function disabledButton (evt) {
    if (this.disabled)
    return false;
    else
    if (this.oldOnClick)
    this.oldOnClick(evt);
    }
    button.onclick = disabledButton;
    Now you can set
    button.disabled = false;
    to disable the button and
    button.disabled = true;
    to enable it. Complete example:
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



    In the these two lines:

    function disabledButton (evt) {
    if (this.disabled)

    Where did the "this" come from and what does it refer to? Also, where
    did the property "disabled" come from???
    lawrence, Nov 6, 2004
    #1
    1. Advertising

  2. On 6 Nov 2004 09:57:16 -0800, lawrence <> wrote:

    [snip]

    > In the these two lines:
    >
    > function disabledButton (evt) {
    > if (this.disabled)
    >
    > Where did the "this" come from and what does it refer to?


    This keyword, this, is an operator. It always references an object, though
    which object depends upon context. In this case, it refers to the button.

    When

    button.onclick = disabledButton;

    is executed, the onclick property of the object, button, refers to the
    function, disabledButton. It becomes a method of that object. When you
    click the button, disabledButton is called and because it is a member of
    button, the this operator refers to that object.

    > Also, where did the property "disabled" come from???


    Hopefully, it's now obvious: it is the disabled property of the button.

    Does that help?

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Nov 6, 2004
    #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. Paul Rogers
    Replies:
    4
    Views:
    407
    Paul Rogers
    Jan 16, 2006
  2. Michael
    Replies:
    4
    Views:
    396
    Matt Hammond
    Jun 26, 2006
  3. lovecreatesbeauty
    Replies:
    17
    Views:
    648
    Keith Thompson
    Jun 16, 2006
  4. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    202
    Jeff Wood
    Sep 29, 2005
  5. Sagar
    Replies:
    0
    Views:
    76
    Sagar
    Jan 18, 2006
Loading...

Share This Page