when to use this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    #1

    when to use this?

    Howdy All!

    Must I *always* precede an instance variable with "this" in JavaScript?

    For example, consider the code below:

    var initialDate;
    function DateControl (initialDate)
    {
    this.initialDat e = initialDate;
    }

    function doSomething()
    {
    alert ("typeof initialDate: " + (typeof initialDate));
    }

    Should initialDate be undefined and this.initialDat e be a date?
    Or should they both be date?

    Thanks for any advice!

    Rob
    :)


  • Lasse Reichstein Nielsen

    #2
    Re: when to use this?

    "Robert Mark Bram" <relaxedrob@rem ove.this.optusn et.com.au> writes:
    [color=blue]
    > Must I *always* precede an instance variable with "this" in JavaScript?[/color]

    If by an "instance variable" you mean a property of an object, then
    you must always[1] access it as part of an object. That object might
    be referenced by the "this" keyword.
    [color=blue]
    > For example, consider the code below:
    >
    > var initialDate;
    > function DateControl (initialDate)
    > {
    > this.initialDat e = initialDate;
    > }
    >
    > function doSomething()
    > {
    > alert ("typeof initialDate: " + (typeof initialDate));
    > }[/color]
    [color=blue]
    > Should initialDate be undefined and this.initialDat e be a date?[/color]

    When this code executes, nothing happens except the creation of two
    functions and one global variable.

    If you call "doSomethin g" after this, the scope rules tells us that
    "initialDat e" inside "doSomethin g" refers to the global variable.
    It is still uninitialized, so the alert reports a type of "undefined" .

    If you create a new object with "new DateControl(foo )", then during
    the execution of the "DateContro l" function, the scope rules tells us
    that "initialDat e" refers to the argument of the "DateContro l"
    function. When using a function as a constructor, the "this" keyword
    refers to the object being created. It then goes on to create a
    property on the newly created object. That property is also called
    "initialDat e", and its value is set to the value of the argument.

    If you call "DateContro l" as a function (not as a constructor with the
    "new" keyword), then inside the body of the function, "this" refers to
    the global object. It then sets the property called "initialDat e" of the
    global object to the value of its argument. The global object contains
    the global variables, so this changes the value of the global variable
    called "initialDat e".
    [color=blue]
    > Or should they both be date?[/color]

    ---
    var x = 4;
    function cx(x) {
    this.x=x;
    }
    var z = new cx("foo");
    var y = new cx(true);
    cx( function(){var x=42;} );

    alert(typeof x + "," + typeof z.x + "," + typeof y.x);
    ---
    What do you expect the alert to say?
    Why?

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Robert Mark Bram

      #3
      Re: when to use this?

      Thank you for the response Lasse!
      [color=blue]
      > What do you expect the alert to say?
      > Why?[/color]

      I was getting confused and not using "this" when I should have been.

      My code had something like:
      var initialDate;
      function DateControl (initialDate)
      {
      this.initialDat e = initialDate;
      this.doSomethin g = doSomething;
      }

      function doSomething()
      {
      alert ("date: " + initialDate);
      }

      I thought JavaScript might be a bit like Java and assume reference to
      "initialDat e" in the doSomething() function would default to the instance
      variable. Now I have learnt my lesson and changed that type code to use the
      "this" reference.

      alert ("date: " + this.initialDat e);


      Rob
      :)


      Comment

      Working...