is 'this' always defined for functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawnmoth

    is 'this' always defined for functions?



    'this' is defined, as I understand it, for event handlers, but why is
    it defined, here?

    Here's the code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>
    <title>this</title>
    </head>

    <body>
    <script type="text/javascript">
    (function() {
    if (this) {
    alert("test exists");
    } else {
    alert("test doesn't exist");
    }
    })();
    </script>
    </body>
    </html>
  • Joost Diepenmaat

    #2
    Re: is 'this' always defined for functions?

    yawnmoth <terra1024@yaho o.comwrites:

    >
    'this' is defined, as I understand it, for event handlers, but why is
    it defined, here?
    >
    Here's the code:
    >
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>
    <title>this</title>
    </head>
    >
    <body>
    <script type="text/javascript">
    (function() {
    if (this) {
    alert("test exists");
    } else {
    alert("test doesn't exist");
    }
    })();
    </script>
    </body>
    </html>
    "this" means the object of a method call. in short, whenever it
    doens't refer to anything more specific, it refers to the global
    object (the "window" object, ignoring some details).

    In method calls on a specific object, "this" refers to the object of
    the call (and DOM events act like method calls in that respect).

    function bla() {
    // do something with "this"
    }

    bla() // act on the global object

    var foo = { bar: bla }

    foo.bla() // act on the foo object

    var zee = {};

    foo.bla.call(z) // act on the zee object


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Joost Diepenmaat

      #3
      Re: is 'this' always defined for functions?

      Joost Diepenmaat <joost@zeekat.n lwrites:
      var zee = {};
      >
      foo.bla.call(z) // act on the zee object
      that should have been

      foo.bla.call(ze e) // act on the zee object

      --
      Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

      Comment

      • yawnmoth

        #4
        Re: is 'this' always defined for functions?

        On Aug 26, 4:20 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
        Joost Diepenmaat <jo...@zeekat.n lwrites:
        var zee = {};
        >
        foo.bla.call(z) // act on the zee object
        >
        that should have been
        >
        foo.bla.call(ze e) // act on the zee object
        Thanks - I appreciate it!

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: is 'this' always defined for functions?

          Joost Diepenmaat wrote:
          "this" means the object of a method call. in short, whenever it
          doens't refer to anything more specific, it refers to the global
          object [...]
          Period. The rest of that paragraph is unnecessary to mention here and
          ultimately misleading.


          PointedEars
          --
          var bugRiddenCrashP ronePieceOfJunk = (
          navigator.userA gent.indexOf('M SIE 5') != -1
          && navigator.userA gent.indexOf('M ac') != -1
          ) // Plone, register_functi on.js:16

          Comment

          • Joost Diepenmaat

            #6
            Re: is 'this' always defined for functions?

            Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
            Joost Diepenmaat wrote:
            >"this" means the object of a method call. in short, whenever it
            >doens't refer to anything more specific, it refers to the global
            >object [...]
            >
            Period. The rest of that paragraph is unnecessary to mention here and
            ultimately misleading.
            You know, it would have been more informative for anyone following
            this thread if you'd quoted the part you objected to, and refered to
            the current thread where you're discussing that subject. This kind of
            reply is not helping anyone.

            Thomas, I respect your knowledge and insight, but I really do not like
            your posting style.

            For future readers: the window/global object thread starts at
            Message-ID: <EK%rk.4889$zv7 .720@flpi143.ff dc.sbc.com>

            --
            Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: is 'this' always defined for functions?

              Joost Diepenmaat wrote:
              Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
              >Joost Diepenmaat wrote:
              >>"this" means the object of a method call. in short, whenever it
              >>doens't refer to anything more specific, it refers to the global
              >>object [...]
              >Period. The rest of that paragraph is unnecessary to mention here and
              >ultimately misleading.
              >
              You know, it would have been more informative for anyone following
              this thread if you'd quoted the part you objected to, and refered to
              the current thread where you're discussing that subject. This kind of
              reply is not helping anyone.
              It would have helped you, would you not have fallen victim to your
              self-delusions.
              Thomas, I respect your knowledge and insight, but I really do not like
              your posting style.
              Go on crying. I am not going to repeat myself just to pamper those who
              cannot seem to follow this simple rule of Usenet: read before you post.


              Score adjusted

              PointedEars
              --
              Use any version of Microsoft Frontpage to create your site.
              (This won't prevent people from viewing your source, but no one
              will want to steal it.)
              -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

              Comment

              • Joost Diepenmaat

                #8
                Re: is 'this' always defined for functions?

                Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                Joost Diepenmaat wrote:
                >Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                >>Joost Diepenmaat wrote:
                >>>"this" means the object of a method call. in short, whenever it
                >>>doens't refer to anything more specific, it refers to the global
                >>>object [...]
                >>Period. The rest of that paragraph is unnecessary to mention here and
                >>ultimately misleading.
                >>
                >You know, it would have been more informative for anyone following
                >this thread if you'd quoted the part you objected to, and refered to
                >the current thread where you're discussing that subject. This kind of
                >reply is not helping anyone.
                >
                It would have helped you, would you not have fallen victim to your
                self-delusions.
                >
                >Thomas, I respect your knowledge and insight, but I really do not like
                >your posting style.
                >
                Go on crying. I am not going to repeat myself just to pamper those who
                cannot seem to follow this simple rule of Usenet: read before you post.
                >
                >
                Score adjusted
                Please adjust it a lot lower, and don't bother replying to me.

                --
                Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: is 'this' always defined for functions?

                  Joost Diepenmaat wrote:
                  Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                  >Joost Diepenmaat wrote:
                  >>Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                  >>>Joost Diepenmaat wrote:
                  >>>>"this" means the object of a method call. in short, whenever it
                  >>>>doens't refer to anything more specific, it refers to the global
                  >>>>object [...]
                  >>>Period. The rest of that paragraph is unnecessary to mention here and
                  >>>ultimately misleading.
                  >>You know, it would have been more informative for anyone following
                  >>this thread if you'd quoted the part you objected to, and refered to
                  >>the current thread where you're discussing that subject. This kind of
                  >>reply is not helping anyone.
                  >It would have helped you, would you not have fallen victim to your
                  >self-delusions.
                  >[...]
                  >Score adjusted
                  >
                  Please adjust it a lot lower, and don't bother replying to me.
                  My apologies, this was uncalled for. While I do not think the thread
                  reference was necessary (we discussed that only hours ago), my quoting
                  was certainly suboptimal as was my second followup here.


                  PointedEars
                  --
                  Anyone who slaps a 'this page is best viewed with Browser X' label on
                  a Web page appears to be yearning for the bad old days, before the Web,
                  when you had very little chance of reading a document written on another
                  computer, another word processor, or another network. -- Tim Berners-Lee

                  Comment

                  • yawnmoth

                    #10
                    Re: is 'this' always defined for functions?

                    On Aug 26, 6:19 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                    wrote:
                    Joost Diepenmaat wrote:
                    "this" means the object of a method call. in short, whenever it
                    doens't refer to anything more specific, it refers to the global
                    object [...]
                    >
                    Period.  The rest of that paragraph is unnecessary to mention here and
                    ultimately misleading.
                    Do they constitute poor coding practices, or something? I actually
                    thought it was useful (didn't know about call(), for instance),
                    although if I should be doing something differently, I'd like to
                    know...

                    Comment

                    • Joost Diepenmaat

                      #11
                      Re: is 'this' always defined for functions?

                      Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                      Joost Diepenmaat wrote:
                      >Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                      >>Joost Diepenmaat wrote:
                      >>>Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
                      >>>>Joost Diepenmaat wrote:
                      >>>You know, it would have been more informative for anyone following
                      >>>this thread if you'd quoted the part you objected to, and refered to
                      >>>the current thread where you're discussing that subject. This kind of
                      >>>reply is not helping anyone.
                      >>It would have helped you, would you not have fallen victim to your
                      >>self-delusions.
                      >>[...]
                      >>Score adjusted
                      >>
                      >Please adjust it a lot lower, and don't bother replying to me.
                      >
                      My apologies, this was uncalled for. While I do not think the thread
                      reference was necessary (we discussed that only hours ago), my quoting
                      was certainly suboptimal as was my second followup here.
                      Apologies accepted.

                      I'm looking forward to more constructive (if occasionally heated)
                      discussions in the future.

                      Joost.

                      --
                      Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: is 'this' always defined for functions?

                        yawnmoth wrote:
                        On Aug 26, 6:19 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
                        >Joost Diepenmaat wrote:
                        >>"this" means the object of a method call. in short, whenever it
                        >>doens't refer to anything more specific, it refers to the global
                        >>object [...]
                        >Period. The rest of that paragraph is unnecessary to mention here and
                        >ultimately misleading.
                        >
                        Do they constitute poor coding practices, or something?
                        Not Joosts examples, they are OK (but see below). (It would turn out my
                        suboptimal quoting was even more misleading than what I was actually
                        referring to. Sigh. [psf 10.1])
                        I actually thought it was useful (didn't know about call(), for
                        instance), although if I should be doing something differently, I'd like
                        to know...
                        Function.protot ype.call() should be feature-tested before called because it
                        is not universally supported:

                        <http://PointedEars.de/es-matrix/#f>


                        HTH

                        PointedEars
                        --
                        Anyone who slaps a 'this page is best viewed with Browser X' label on
                        a Web page appears to be yearning for the bad old days, before the Web,
                        when you had very little chance of reading a document written on another
                        computer, another word processor, or another network. -- Tim Berners-Lee

                        Comment

                        • Joost Diepenmaat

                          #13
                          Re: is 'this' always defined for functions?

                          yawnmoth <terra1024@yaho o.comwrites:
                          On Aug 26, 6:19 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                          wrote:
                          >Joost Diepenmaat wrote:
                          "this" means the object of a method call. in short, whenever it
                          doens't refer to anything more specific, it refers to the global
                          object [...]
                          >>
                          >Period.  The rest of that paragraph is unnecessary to mention here and
                          >ultimately misleading.
                          >
                          Do they constitute poor coding practices, or something? I actually
                          thought it was useful (didn't know about call(), for instance),
                          although if I should be doing something differently, I'd like to
                          know...
                          Thomas has (reasonably justifiable) objections to calling the "window"
                          object the global object. See the 'Why "window.alert() " over
                          "alert()"?' thread (I linked to it in this one).

                          --
                          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                          Comment

                          • yawnmoth

                            #14
                            Re: is 'this' always defined for functions?

                            On Aug 26, 7:46 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                            wrote:
                            yawnmoth wrote:
                            On Aug 26, 6:19 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
                            Joost Diepenmaat wrote:
                            >"this" means the object of a method call. in short, whenever it
                            >doens't refer to anything more specific, it refers to the global
                            >object [...]
                            Period.  The rest of that paragraph is unnecessary to mention here and
                            ultimately misleading.
                            >
                            Do they constitute poor coding practices, or something?
                            >
                            Not Joosts examples, they are OK (but see below).  (It would turn out my
                            suboptimal quoting was even more misleading than what I was actually
                            referring to.  Sigh. [psf 10.1])
                            >
                            I actually thought it was useful (didn't know about call(), for
                            instance), although if I should be doing something differently, I'd like
                            to know...
                            >
                            Function.protot ype.call() should be feature-tested before called because it
                            is not universally supported:
                            >
                            <http://PointedEars.de/es-matrix/#f>
                            Thanks! I'll need to read that "Why "window.alert() " over
                            "alert()"?" thread Joost mentioned :)

                            Comment

                            Working...