SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does the "this" mean?

    Especially when it is used as an argument of a function (in actions like onclick,......)

  2. #2
    Non-Member mmi's Avatar
    Join Date
    Jun 2001
    Location
    Rhode Island
    Posts
    587
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey again - I can't script mmyself (I just hang out with people who can), so all I can offer is some material from Netscape, the folks who created this stuff
    "this" is a keyword that you can use to refer to the current object. In general, in a method this refers to the calling object.
    Syntax
    this[.propertyName]
    Examples:
    Suppose a function called validate validates an object's value property, given the object and the high and low values:
    function validate(obj, lowval, hival) {
    if ((obj.value < lowval) || (obj.value > hival))
    alert("Invalid Value!" )
    }
    You could call validate in each form element's onChange event handler, using this to pass it the form element, as in the following example:
    <B>Enter a number between 18 and 99/B>
    <INPUT TYPE = "text" NAME = "age" SIZE = 3
    onChange="validate(this, 18, 99)">
    more

  3. #3
    SitePoint Guru bronze trophy blufive's Avatar
    Join Date
    Mar 2002
    Location
    Manchester, UK
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Netscape didn't invent "this" - it's a fundamental concept in object-oriented programming.

  4. #4
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blufive
    Netscape didn't invent "this" - it's a fundamental concept in object-oriented programming.
    ...and I've been using it since before Netscape existed.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  5. #5
    Non-Member mmi's Avatar
    Join Date
    Jun 2001
    Location
    Rhode Island
    Posts
    587
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb browsers, frames, layers, no charge, open source, ...

    Quote Originally Posted by blufive
    Netscape didn't invent "this" - it's a fundamental concept in object-oriented programming.
    you misread mmy meaning - I was not suggesting that Netscapers established the use of this as a programming tool, but rather that they created JavaScript

    http://www.howtocreate.co.uk/jshistory.html

  6. #6
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    However, Netscape DID create JavaScript, which then became standardized as ECMAScript.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops - had had this thread open for a while before posting... mmi posted in the meantime...!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jonsof
    Especially when it is used as an argument of a function (in actions like onclick,......)
    The "this" keyword literally means "this object". So like when you see code like the following:

    <div OnMouseOver="applyTo(this)">

    The this keyword basically tells the browser to apply the applyTo() function to the div tag it is written into.


Bookmarks

Posting Permissions

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