473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A question on this

dmjpro
2,476 2GB
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
9 1108
hsriat
1,654 Expert 1GB
"this" is the string to which you are applying the function, and this[i] is this.charAt(i)
Aug 5 '08 #2
dmjpro
2,476 2GB
"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
1,654 Expert 1GB
What do you want me to explain in this? The code is straight forward to reverse a string.
Aug 5 '08 #4
dmjpro
2,476 2GB
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
427 Expert 256MB
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
2,476 2GB
Thanks Rnd me, but my question is..how this[index] works?
Aug 5 '08 #7
rnd me
427 Expert 256MB
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
2,476 2GB
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
5,390 Expert Mod 4TB
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

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

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Dan Foley | last post by:
This script runs fine, but I'd like to know why it's so slow.. Thanks for any help out there on how i can make it faster (it might take up to 5 min to write these 3 export files whith 15 records...
5
by: Lee Redeem | last post by:
Hi there I've created abd uploaded this basic PHP script: <html> <head> <title>PHP Test</title> </head> <body> <H1 align="center">
5
by: christopher vogt | last post by:
Hi, i'm wondering if there is something like $this-> to call a method inside another method of the same class without using the classname in front. I actually use class TEST { function...
6
by: Phil Powell | last post by:
Ok guys, here we go again! SELECT s.nnet_produkt_storrelse_navn FROM nnet_produkt_storrelse s, nnet_produkt_varegruppe v, nnet_storrelse_varegruppe_assoc sv, nnet_produkt p WHERE...
1
by: Michel | last post by:
a site like this http://www.dvdzone2.com/dvd Can you make it in PHP and MySQL within 6 weeks? If so, send me your price 2 a r a (at) p a n d o r a . b e
11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.