By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,483 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,483 IT Pros & Developers. It's quick & easy.

A question on this

dmjpro
100+
P: 2,476
here my code goes....

Expand|Select|Wrap|Line Numbers
  1. String.prototype.reversed = function() {
  2.     var r = "";
  3.     for (var i = this.length - 1; i >= 0; i--) {
  4.         r += this[i];
  5.     }
  6.     return r;
  7. }
  8.  
How does this[i] mean?
Aug 5 '08 #1
Share this Question
Share on Google+
9 Replies


hsriat
Expert 100+
P: 1,654
"this" is the string to which you are applying the function, and this[i] is this.charAt(i)
Aug 5 '08 #2

dmjpro
100+
P: 2,476
"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?
Aug 5 '08 #3

hsriat
Expert 100+
P: 1,654
What do you want me to explain in this? The code is straight forward to reverse a string.
Aug 5 '08 #4

dmjpro
100+
P: 2,476
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?
Aug 5 '08 #5

rnd me
Expert 100+
P: 427
when used in a prototype, as in String.prototype.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.prototype.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.reversed()", that would be "name", so this===name in that invocation.


make any sense?
Aug 5 '08 #6

dmjpro
100+
P: 2,476
Thanks Rnd me, but my question is..how this[index] works?
Aug 5 '08 #7

rnd me
Expert 100+
P: 427
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]
Aug 5 '08 #8

dmjpro
100+
P: 2,476
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.
Aug 5 '08 #9

gits
Expert Mod 5K+
P: 5,343
the 'native'-js type String is a 'kind of Array' so that you may ask for its length:

Expand|Select|Wrap|Line Numbers
  1. var s = 'foo'
  2. alert(s.length);
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. var s = 'foo';
  2.  
  3. String.prototype.x = function() { };
  4.  
  5. // this will be 3
  6. var l = s.length;
  7.  
  8. // refers to the function
  9. s['x'] === s.x;
  10.  
  11. // refers to the first property of the string's value
  12. s[0] == 'f';
  13.  
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?

Expand|Select|Wrap|Line Numbers
  1. function MY_STRING(val) {
  2.     this.length = val.length;
  3.  
  4.     for (var i = 0; i < this.length; i++) {
  5.         this[i] = val[i];
  6.     }    
  7. }
  8.  
  9. var o = new MY_STRING('foo');
  10.  
  11. alert(o[0]);
kind regards
Aug 5 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.