Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37197 > unrolled thread
| Started by | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| First post | 2019-09-10 11:41 -0700 |
| Last post | 2019-09-11 13:48 +0200 |
| Articles | 14 — 4 participants |
Back to article view | Back to comp.lang.javascript
Create a string dynamically to reference a property of an object? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-09-10 11:41 -0700
Re: Create a string dynamically to reference a property of an object? Julio Di Egidio <julio@diegidio.name> - 2019-09-10 12:38 -0700
Re: Create a string dynamically to reference a property of an object? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-09-10 14:02 -0700
Re: Create a string dynamically to reference a property of an object? dr.j.r.stockton@gmail.com - 2019-09-11 12:58 -0700
Re: Create a string dynamically to reference a property of an object? "R.Wieser" <address@not.available> - 2019-09-10 21:59 +0200
Re: Create a string dynamically to reference a property of an object? Julio Di Egidio <julio@diegidio.name> - 2019-09-10 13:04 -0700
Re: Create a string dynamically to reference a property of an object? "R.Wieser" <address@not.available> - 2019-09-11 09:33 +0200
Re: Create a string dynamically to reference a property of an object? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-09-11 00:52 -0700
Re: Create a string dynamically to reference a property of an object? "R.Wieser" <address@not.available> - 2019-09-11 10:11 +0200
Re: Create a string dynamically to reference a property of an object? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-09-11 02:09 -0700
Re: Create a string dynamically to reference a property of an object? "R.Wieser" <address@not.available> - 2019-09-11 13:59 +0200
Re: Create a string dynamically to reference a property of an object? Julio Di Egidio <julio@diegidio.name> - 2019-09-11 02:11 -0700
Re: Create a string dynamically to reference a property of an object? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-09-11 03:48 -0700
Re: Create a string dynamically to reference a property of an object? "R.Wieser" <address@not.available> - 2019-09-11 13:48 +0200
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2019-09-10 11:41 -0700 |
| Subject | Create a string dynamically to reference a property of an object? |
| Message-ID | <aba8a905-8515-4be8-9e03-040f60d109e5@googlegroups.com> |
Well i was apparently wrong it is not a variable, maybe a property of an object?
This was my attempt, and it is wrong.
varString="document.getElementById.('inBox"+x+"').checked";
console.log("***"+varString);
var myVar = eval(varString);
So how do i go on create these type of "object property references?".
[toc] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-09-10 12:38 -0700 |
| Message-ID | <836c3655-6474-4b99-912d-013945b88482@googlegroups.com> |
| In reply to | #37197 |
On Tuesday, 10 September 2019 20:41:39 UTC+2, Jonas Thörnvall wrote:
> Well i was apparently wrong it is not a variable, maybe a property of an object?
>
> This was my attempt, and it is wrong.
> varString="document.getElementById.('inBox"+x+"').checked";
An extra dot from an extra troll.
> console.log("***"+varString);
> var myVar = eval(varString);
>
> So how do i go on create these type of "object property references?".
Nobody said document.getElementById only takes a literal constant string
as argument, i.e. you can just do:
var myVar = document.getElementById("inBox" + x).checked;
The same goes for object property access: if you know the property name,
you just write myObj.someProp, but if the property name is only computable
at runtime, you just write myObj[somePropName] where somePropName again
can be any expression that eventually evaluates to a string. (And keep
in mind that in JS objects are essentially dictionaries.)
Indeed, note that there is no reason on Earth why you'd use eval in your
code ever.
HTH,
Julio
[toc] | [prev] | [next] | [standalone]
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2019-09-10 14:02 -0700 |
| Message-ID | <37cc9c88-a566-420c-8000-f5a7ae87351d@googlegroups.com> |
| In reply to | #37198 |
Den tisdag 10 september 2019 kl. 21:38:39 UTC+2 skrev Julio Di Egidio:
> On Tuesday, 10 September 2019 20:41:39 UTC+2, Jonas Thörnvall wrote:
> > Well i was apparently wrong it is not a variable, maybe a property of an object?
> >
> > This was my attempt, and it is wrong.
> > varString="document.getElementById.('inBox"+x+"').checked";
>
> An extra dot from an extra troll.
>
> > console.log("***"+varString);
> > var myVar = eval(varString);
> >
> > So how do i go on create these type of "object property references?".
>
> Nobody said document.getElementById only takes a literal constant string
> as argument, i.e. you can just do:
>
> var myVar = document.getElementById("inBox" + x).checked;
>
> The same goes for object property access: if you know the property name,
> you just write myObj.someProp, but if the property name is only computable
> at runtime, you just write myObj[somePropName] where somePropName again
> can be any expression that eventually evaluates to a string. (And keep
> in mind that in JS objects are essentially dictionaries.)
>
> Indeed, note that there is no reason on Earth why you'd use eval in your
> code ever.
>
> HTH,
>
> Julio
I thought that ("inBox" + x) did look peculiar in middle of an "object property reference?". But apparently javascript interpretate the expression between brackets.
[toc] | [prev] | [next] | [standalone]
| From | dr.j.r.stockton@gmail.com |
|---|---|
| Date | 2019-09-11 12:58 -0700 |
| Message-ID | <d300ba4e-3d6f-4666-a0c0-2a4dd0a0fc51@googlegroups.com> |
| In reply to | #37198 |
On Tuesday, 10 September 2019 20:38:39 UTC+1, Julio Di Egidio wrote: > Indeed, note that there is no reason on Earth why you'd use eval in your > code ever. I have a local page which contains an editable textarea. Its actions are entirely self-contained; its only I/O is keyboard/screen. The users' intents are entirely benign. The idea is that one can enter multiple arithmetic expressions, or fragments of ECMAScript, to see what they mean or do. How would you code this without using eval and without writing your own interpreter? -- (c) John Stockton, near London, UK. Using Google Groups. | Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-09-10 21:59 +0200 |
| Message-ID | <ql8vb5$iqr$1@gioia.aioe.org> |
| In reply to | #37197 |
Jonas,
> varString="document.getElementById.('inBox"+x+"').checked";
Remove the "." just infront of the "(" and try again.
Hint: When something doesn't work /go back to the simpelest form you know/ -
in this case trying to execute it directly, and with any additions like
dynamic ID names:
console.log("***"+ document.getElementById.('inBox0').checked );
The above wil fail too, due to that extra ".". But with a bit of luck (and
the right tools, which you already seem to use) you will get a lot more info
about /what/ is going wrong, and can repair it yourself.
And by the way: I hope you do realize that the element id you are using
itself is, after you create that string, not dynamic but hardcoded.
Ackk .. I just remembered something ...
And #&$&*^$ @ !!
Forget that. I just realized that your explanation was inprecise enough to
make me think your "object property references" was referring to the
".checked" part. Looking at your previous (differently named, so a bit of
a gamble) post thats not quite likely. Give yourself a "well done" slap
on the shoulder, you just made me waste my time. :-(
And that also means that if that element id is the /only/ thing thats
dynamic you do not even need to go thru storing it into a variable and than
eval-ing it. It will work perfectly whe you use it directly:
x=1
var myVar = document.getElementById('inBox'"+x+"').checked;
So much for not first trying the simpelest approach first, eh ? <whistle>
:-)
Regards,
Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-09-10 13:04 -0700 |
| Message-ID | <d982f5c8-87f9-4fd7-a03e-3f18bdfcb718@googlegroups.com> |
| In reply to | #37200 |
On Tuesday, 10 September 2019 21:59:40 UTC+2, R.Wieser wrote:
> Jonas,
>
> > varString="document.getElementById.('inBox"+x+"').checked";
>
> Remove the "." just infront of the "(" and try again.
>
> Hint: When something doesn't work /go back to the simpelest form you know/ -
> in this case trying to execute it directly, and with any additions like
> dynamic ID names:
>
> console.log("***"+ document.getElementById.('inBox0').checked );
>
> The above wil fail too, due to that extra ".". But with a bit of luck (and
> the right tools, which you already seem to use) you will get a lot more info
> about /what/ is going wrong, and can repair it yourself.
>
> And by the way: I hope you do realize that the element id you are using
> itself is, after you create that string, not dynamic but hardcoded.
>
> Ackk .. I just remembered something ...
>
> And #&$&*^$ @ !!
>
> Forget that. I just realized that your explanation was inprecise enough to
> make me think your "object property references" was referring to the
> ".checked" part. Looking at your previous (differently named, so a bit of
> a gamble) post thats not quite likely. Give yourself a "well done" slap
> on the shoulder, you just made me waste my time. :-(
>
> And that also means that if that element id is the /only/ thing thats
> dynamic you do not even need to go thru storing it into a variable and than
> eval-ing it. It will work perfectly whe you use it directly:
>
> x=1
> var myVar = document.getElementById('inBox'"+x+"').checked;
>
> So much for not first trying the simpelest approach first, eh ? <whistle>
> :-)
So much for not even getting the quotes right.
They always come in pairs...
*Plonk*
Julio
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-09-11 09:33 +0200 |
| Message-ID | <qla7vp$1tib$1@gioia.aioe.org> |
| In reply to | #37201 |
Julio,
> So much for not even getting the quotes right.
>
> They always come in pairs...
I realized when I got up this moring that after I had copy-pasted the
origional line I forgot to clean up that part in my last code fragment.
But because of such a mistake you throw away /everything/ ? Good going
kido, good going. /s
Also, you where so focussed on chatising me that you forgot to do what you
(I assume, but start to doubt it) came for here: Help jonas with his
problem. In this case showing him how to fix my suggestion so it would
work for him. Again, good going. Not.
@Jonas,
As Julio has been complaining, I made a mess of the last code fragment.
The('inBox'"+x+"') should have been just ('inBox'+x). So:
x=1
var myVar = document.getElementById('inBox'+x).checked;
Regards,
Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2019-09-11 00:52 -0700 |
| Message-ID | <db45023c-8824-4c4f-8ed2-4bbd1ede94ec@googlegroups.com> |
| In reply to | #37215 |
Den onsdag 11 september 2019 kl. 09:33:19 UTC+2 skrev R.Wieser:
> Julio,
>
> > So much for not even getting the quotes right.
> >
> > They always come in pairs...
>
> I realized when I got up this moring that after I had copy-pasted the
> origional line I forgot to clean up that part in my last code fragment.
>
> But because of such a mistake you throw away /everything/ ? Good going
> kido, good going. /s
>
> Also, you where so focussed on chatising me that you forgot to do what you
> (I assume, but start to doubt it) came for here: Help jonas with his
> problem. In this case showing him how to fix my suggestion so it would
> work for him. Again, good going. Not.
>
> @Jonas,
>
> As Julio has been complaining, I made a mess of the last code fragment.
> The('inBox'"+x+"') should have been just ('inBox'+x). So:
>
> x=1
> var myVar = document.getElementById('inBox'+x).checked;
>
>
> Regards,
> Rudy Wieser
Well i am thankful for help with the syntax of javascript at any level, so thanks to both Julio and you.
I was a bit surprised i must say that the interpretator actually do interpretate the expression between brackets "pleasantly surprised", now i just have to remember it.
I was pretty sure i had to eval the full string. "It also seem to work".
Thanks anyhow Julio and Rudy
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-09-11 10:11 +0200 |
| Message-ID | <qlaa8g$81f$1@gioia.aioe.org> |
| In reply to | #37218 |
Jonas,
> I was a bit surprised i must say that the interpretator actually
> do interpretate the expression between brackets "pleasantly
> surprised", now i just have to remember it.
:-) For the record: AFAICT everywhere where you can put in an argument you
can also put in an expression. And that means that, apart from static
strings and/or variables, you also can call other functions there.
Like this:
- - - - - - - - - - - - - - - - - - - -
function MultiplyByTwo(arg1) {
return arg1 * 2
}
var myVar = document.getElementById( 'inBox' + MultiplyByTwo(3) ).checked;
- - - - - - - - - - - - - - - - - - - -
Regards,
Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2019-09-11 02:09 -0700 |
| Message-ID | <0955ab6c-1da6-4321-a15f-e5613d9a5ae5@googlegroups.com> |
| In reply to | #37219 |
Den onsdag 11 september 2019 kl. 10:12:07 UTC+2 skrev R.Wieser:
> Jonas,
>
> > I was a bit surprised i must say that the interpretator actually
> > do interpretate the expression between brackets "pleasantly
> > surprised", now i just have to remember it.
>
> :-) For the record: AFAICT everywhere where you can put in an argument you
> can also put in an expression. And that means that, apart from static
> strings and/or variables, you also can call other functions there.
>
> Like this:
> - - - - - - - - - - - - - - - - - - - -
>
> function MultiplyByTwo(arg1) {
> return arg1 * 2
> }
>
> var myVar = document.getElementById( 'inBox' + MultiplyByTwo(3) ).checked;
>
> - - - - - - - - - - - - - - - - - - - -
>
> Regards,
> Rudy Wieser
It looks kind of strange though, if i stumbled upon it i would not know what to think about it.
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-09-11 13:59 +0200 |
| Message-ID | <qlanjn$644$1@gioia.aioe.org> |
| In reply to | #37224 |
Jonas, > It looks kind of strange though, if i stumbled upon it i > would not know what to think about it. :-) That means I gave you the perfect example for the occasion. And by the way, that example, in one form or another, is valid for most scripting (and a number high(er)-level programming) languages too. Regards, Rudy Wieser
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2019-09-11 02:11 -0700 |
| Message-ID | <2b46d57a-3b9a-4511-b0b2-9cc8734aef7e@googlegroups.com> |
| In reply to | #37215 |
On Wednesday, 11 September 2019 09:33:19 UTC+2, R.Wieser wrote: > Julio, > > > So much for not even getting the quotes right. > > > > They always come in pairs... > > I realized when I got up this moring that after I had copy-pasted the > origional line I forgot to clean up that part in my last code fragment. > > But because of such a mistake you throw away /everything/ ? Good going > kido, good going. /s Everything what you stupid fuck? I had posted a much more comprehensive, not to mention precise, answer than yours, and you utter moron, instead of STFU and learning something for a change, or at least trying to build from it to create, God forgive, some synergy, no: you just go on and post on top of it as if nothing had been said. Is that what you mean by good going, you stupid fuck with nothing better to do than spamming the Usenet?? You are welcome. *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2019-09-11 03:48 -0700 |
| Message-ID | <8be4c3c7-ff0d-4b9f-a498-0bae9f0a80ae@googlegroups.com> |
| In reply to | #37225 |
Den onsdag 11 september 2019 kl. 11:12:04 UTC+2 skrev Julio Di Egidio: > On Wednesday, 11 September 2019 09:33:19 UTC+2, R.Wieser wrote: > > Julio, > > > > > So much for not even getting the quotes right. > > > > > > They always come in pairs... > > > > I realized when I got up this moring that after I had copy-pasted the > > origional line I forgot to clean up that part in my last code fragment. > > > > But because of such a mistake you throw away /everything/ ? Good going > > kido, good going. /s > > Everything what you stupid fuck? I had posted a much more comprehensive, > not to mention precise, answer than yours, and you utter moron, instead of > STFU and learning something for a change, or at least trying to build from > it to create, God forgive, some synergy, no: you just go on and post on top > of it as if nothing had been said. Is that what you mean by good going, you > stupid fuck with nothing better to do than spamming the Usenet?? > > You are welcome. > > *Plonk* > > Julio Be nice children don't fight, focus on answer my questions instead, the task at hand is everything save your little quarells for bedtime LoL ;)
[toc] | [prev] | [next] | [standalone]
| From | "R.Wieser" <address@not.available> |
|---|---|
| Date | 2019-09-11 13:48 +0200 |
| Message-ID | <qlamu4$36l$1@gioia.aioe.org> |
| In reply to | #37225 |
Julio, > Everything what you stupid fuck? I had posted My apologies, I see that you did that even before I posted mine. ... but about the rest of that sentence ? I sincerely doubt it. > The same goes for object property access Lolz. You, mister teriffic, fell into the same pit I (almost!) did - not quite knowing what Jonas was referring to. So much for your superiority I guess. :-p > *Plonk* How many times are you going to do that ? Before /actually/ doing it I mean. 'Cause I could do well without an agressive buthole like you. Hey, maybe I can get my 'puter to "plonk" /you/ ! I'll just go upstairs and ask my ma how to do that. :-) And by the way: > An extra dot from an extra troll. You /do/ realize that you where talking to Jonas there, don't you ? Either you made a mistake there (just like us "common" people), or you are trying to pick a fight with both of us (thats what I call a lose-lose situation :-) ) Regards, Rudy Wieser
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web