Am I using 'this' to often?

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

    Am I using 'this' to often?

    Being nebie in Javascript i am trying to code my own helper object
    that does pagination.
    So here is a snippet of my code.

    MyData.prototyp e = {
    ....blablabla.. .
    PageUp: function() {
    this.iFrom += this.pageSize;
    this.CheckPageI ndex();
    },

    CheckPageIndex: function() {
    if( this.iFrom >= this.data.lengt h )
    this.iFrom = Math.floor((thi s.data.length - 1)/
    this.pageSize) * this.pageSize;

    if( this.iFrom < 0 )
    this.iFrom = 0;
    }
    }

    Why do I need to call CheckPageIndex using this.CheckPageI ndex when
    called from PageUp? It's in the same object...

    Without 'this' I get an error 'CheckPageIndex is undefined'. Coming
    from object oriented languages like C++ I have a trouble understanding
    it.
    Or am I doing it wrong and there is a way not to specify 'this' to
    many times?
  • Lasse Reichstein Nielsen

    #2
    Re: Am I using 'this' to often?

    George <gevorg@comcast .netwrites:
    MyData.prototyp e = {
    ....blablabla.. .
    PageUp: function() {
    this.iFrom += this.pageSize;
    this.CheckPageI ndex();
    },
    >
    CheckPageIndex: function() {
    if( this.iFrom >= this.data.lengt h )
    this.iFrom = Math.floor((thi s.data.length - 1)/
    this.pageSize) * this.pageSize;
    >
    if( this.iFrom < 0 )
    this.iFrom = 0;
    }
    }
    >
    Why do I need to call CheckPageIndex using this.CheckPageI ndex when
    called from PageUp? It's in the same object...
    Being on the same object doesn't affect the scope. You have to use
    "this" to access properties of the current object.
    Without 'this' I get an error 'CheckPageIndex is undefined'. Coming
    from object oriented languages like C++ I have a trouble understanding
    it.
    It's not complex, just primitive.
    In C++, if the Foo class has a foo_ property, a method on the Foo class
    can refer to it as just "foo_". It can also refer to it as "this->foo_".

    Javascript only allows the latter, where "this" is a reference instead
    of a pointer, so you write "this.foo".
    Or am I doing it wrong and there is a way not to specify 'this' to
    many times?
    In this case, I don't think so.
    If I read the same property a lot of times, I will copy it to a local
    variable:
    Foo.prototype.f oo = function() {
    var bar = this.bar;
    ....bar...bar.. ..bar...;
    }
    In these examples, you use each property two times, or write it between
    reads, and the code is fairly short, so I wouldn't bother doing anything.

    /L
    --
    Lasse Reichstein Holst Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • George

      #3
      Re: Am I using 'this' to often?

      On Nov 10, 11:43 am, Lasse Reichstein Nielsen <lrn.unr...@gma il.com>
      wrote:
      George <gev...@comcast .netwrites:
      MyData.prototyp e = {
       ....blablabla.. .
         PageUp: function() {
              this.iFrom += this.pageSize;
              this.CheckPageI ndex();
          },
      >
          CheckPageIndex: function() {
              if( this.iFrom >= this.data.lengt h )
                  this.iFrom = Math.floor((thi s.data.length - 1)/
      this.pageSize) * this.pageSize;
      >
              if( this.iFrom < 0 )
                  this.iFrom = 0;
          }
      }
      >
      Why do I need to call CheckPageIndex using this.CheckPageI ndex when
      called from PageUp? It's in the same object...
      >
      Being on the same object doesn't affect the scope. You have to use
      "this" to access properties of the current object.
      >
      Without 'this' I get an error 'CheckPageIndex is undefined'. Coming
      from object oriented languages like C++ I have a trouble understanding
      it.
      >
      It's not complex, just primitive.
      In C++, if the Foo class has a foo_ property, a method on the Foo class
      can refer to it as just "foo_". It can also refer to it as "this->foo_".
      >
      Javascript only allows the latter, where "this" is a reference instead
      of a pointer, so you write "this.foo".
      >
      Or am I doing it wrong and there is a way not to specify 'this' to
      many times?
      >
      In this case, I don't think so.
      If I read the same property a lot of times, I will copy it to a local
      variable:
       Foo.prototype.f oo = function() {
         var bar = this.bar;
         ....bar...bar.. ..bar...;
       }
      In these examples, you use each property two times, or write it between
      reads, and the code is fairly short, so I wouldn't bother doing anything.
      >
      /L
      --
      Lasse Reichstein Holst Nielsen
       DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'- Hide quoted text -
      >
      - Show quoted text -
      Thanks a lot.
      George.

      Comment

      • Dr J R Stockton

        #4
        Re: Am I using 'this' to often?

        In comp.lang.javas cript message <97de74fe-70a5-422e-be32-033aa2cd0ddf@u2
        9g2000pro.googl egroups.com>, Mon, 10 Nov 2008 07:31:20, George
        <gevorg@comcast .netposted:
        >
        >Why do I need to call CheckPageIndex using this.CheckPageI ndex when
        >called from PageUp? It's in the same object...
        >
        You can at least approximate to the behaviour elsewhere by using

        with (this) { ... ... ... }

        although some will consider that unwise.

        --
        (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk DOS 3.3 6.20 ; WinXP.
        Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links.
        PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
        My DOS <URL:http://www.merlyn.demo n.co.uk/batfiles.htm- also batprogs.htm.

        Comment

        Working...