A question on this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    A question on this

    here my code goes....

    [code=javascript]
    String.prototyp e.reversed = function() {
    var r = "";
    for (var i = this.length - 1; i >= 0; i--) {
    r += this[i];
    }
    return r;
    }
    [/code]

    How does this[i] mean?
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    "this" is the string to which you are applying the function, and this[i] is this.charAt(i)

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by hsriat
      "this" is the string to which you are applying the function, and this[i] is this.charAt(i)
      How the coding done inside the String Object?
      That's what i asking, i knew that this[i] means the elements at index i.
      Could you explain?

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        What do you want me to explain in this? The code is straight forward to reverse a string.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by hsriat
          What do you want me to explain in this? The code is straight forward to reverse a string.
          Don't misunderstand me ...!
          How do this[i] works?

          Comment

          • rnd me
            Recognized Expert Contributor
            • Jun 2007
            • 427

            #6
            when used in a prototype, as in String.prototyp e.reversed, "this" refers to the object of the type identified by the constructor to the left of the first dot before
            "prototype" .

            what the hell does that mean?

            well, in "String.prototy pe.reversed", "String" is to the left of .prototype.
            what this means to the script is that all objects constructed by String, strings, ( globals, properties of objects, etc) inherit a reversed method.

            if a string does not have it's own property "reversed", its prototype is checked, and if a match is found, the method is invoked. at this point, "this" refers to the object identified to the left of the right-most dot. in "me.name.revers ed()", that would be "name", so this===name in that invocation.


            make any sense?

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Thanks Rnd me, but my question is..how this[index] works?

              Comment

              • rnd me
                Recognized Expert Contributor
                • Jun 2007
                • 427

                #8
                Originally posted by dmjpro
                Thanks Rnd me, but my question is..how this[index] works?

                ok, i am lost as to what you are asking.

                a stab:

                var prop = "bar";
                foo.bar === foo['bar'] === foo[prop]

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Originally posted by rnd me
                  ok, i am lost as to what you are asking.

                  a stab:

                  var prop = "bar";
                  foo.bar === foo['bar'] === foo[prop]
                  I am not still getting you ...please make me understand.

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    the 'native'-js type String is a 'kind of Array' so that you may ask for its length:

                    [CODE=javascript]var s = 'foo'
                    alert(s.length) ;
                    [/CODE]
                    when you extend the String's prototype you add a custom method to it and using a for-in-loop will show you that method additionally while using the standard for-var-i-=-0-... loop will work on the original String-'Array'-like value ... the length is the length of that value but the custom method is a additional property ... that even is the case with the native JS Array-type or DOM-Node-collections - so the you may have a look at the following example:

                    [CODE=javascript]var s = 'foo';

                    String.prototyp e.x = function() { };

                    // this will be 3
                    var l = s.length;

                    // refers to the function
                    s['x'] === s.x;

                    // refers to the first property of the string's value
                    s[0] == 'f';
                    [/CODE]
                    i don't know exactly whether 0 is a String-objects property internally but perhaps its handled that way so you could imagine a String-Object like this?

                    [CODE=javascript]function MY_STRING(val) {
                    this.length = val.length;

                    for (var i = 0; i < this.length; i++) {
                    this[i] = val[i];
                    }
                    }

                    var o = new MY_STRING('foo' );

                    alert(o[0]);[/CODE]
                    kind regards

                    Comment

                    Working...