Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #7613 > unrolled thread
| Started by | Swifty <steve.j.swift@gmail.com> |
|---|---|
| First post | 2011-10-22 11:56 +0100 |
| Last post | 2011-10-23 21:16 +0100 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.javascript
Get value of named object Swifty <steve.j.swift@gmail.com> - 2011-10-22 11:56 +0100
Re: Get value of named object "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-10-22 11:20 +0000
Re: Get value of named object Swifty <steve.j.swift@gmail.com> - 2011-10-22 18:57 +0100
Re: Get value of named object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-22 21:20 +0200
Re: Get value of named object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-22 14:35 +0200
Re: Get value of named object Swifty <steve.j.swift@gmail.com> - 2011-10-22 18:59 +0100
Re: Get value of named object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-22 21:22 +0200
Re: Get value of named object Scott Sauyet <scott.sauyet@gmail.com> - 2011-10-22 12:53 -0700
Re: Get value of named object Lasse Reichstein Nielsen <lrn.unread@gmail.com> - 2011-10-23 02:10 +0200
Re: Get value of named object Swifty <steve.j.swift@gmail.com> - 2011-10-23 09:01 +0100
Re: Get value of named object Lasse Reichstein Nielsen <lrn.unread@gmail.com> - 2011-10-23 11:57 +0200
Re: Get value of named object Swifty <steve.j.swift@gmail.com> - 2011-10-24 14:11 +0100
Re: Get value of named object Dr J R Stockton <reply1142@merlyn.demon.co.uk> - 2011-10-23 21:16 +0100
| From | Swifty <steve.j.swift@gmail.com> |
|---|---|
| Date | 2011-10-22 11:56 +0100 |
| Subject | Get value of named object |
| Message-ID | <a185a7pdddj3ui58j7a8rrtuivpe4gu92l@4ax.com> |
If I have the name of an object in a variable, how would I go about getting the value of that object? Example: varname = 'navigator.appName'; How would I get the value of navigator.appName ? See http://swiftys.org.uk/jsvars.html for my first, faltering attempt. The idea is to replace the multiple "document.write" lines with the simpler "show()" calls. -- Steve Swift http://www.swiftys.org.uk/swifty.html http://www.ringers.org.uk
[toc] | [next] | [standalone]
| From | "Evertjan." <exjxw.hannivoort@interxnl.net> |
|---|---|
| Date | 2011-10-22 11:20 +0000 |
| Message-ID | <Xns9F8687C70E464eejj99@194.109.133.133> |
| In reply to | #7613 |
Swifty wrote on 22 okt 2011 in comp.lang.javascript: > If I have the name of an object in a variable, how would I go about > getting the value of that object? myObject.name perhaps would return the name of an object. > > Example: > varname = 'navigator.appName'; > > How would I get the value of navigator.appName ? > > See http://swiftys.org.uk/jsvars.html for my first, faltering attempt. > > The idea is to replace the multiple "document.write" lines with the > simpler "show()" calls. document.write( "<TR><TH>" + varname + "<TD>" + eval(varname) ); ================= Using eval() indeed is a variable blessing: <H1>JavaScript Varibles</H1> -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Swifty <steve.j.swift@gmail.com> |
|---|---|
| Date | 2011-10-22 18:57 +0100 |
| Message-ID | <js06a7ppjhchhf6dplijj95borph2dd23q@4ax.com> |
| In reply to | #7614 |
On 22 Oct 2011 11:20:48 GMT, "Evertjan." <exjxw.hannivoort@interxnl.net> wrote: >Using eval() indeed is a variable blessing: > ><H1>JavaScript Varibles</H1> Thanks for that, and eval() I realise that eval() is disliked, as is the "Interpret" instruction which does something similar in my usual language. -- Steve Swift http://www.swiftys.org.uk/swifty.html http://www.ringers.org.uk
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-10-22 21:20 +0200 |
| Message-ID | <2077935.sOKSSo7yd4@PointedEars.de> |
| In reply to | #7621 |
Swifty wrote: > […] "Evertjan." wrote: >> Using eval() indeed is a variable blessing: >> >> <H1>JavaScript Varibles</H1> > > Thanks for that, and eval() > > I realise that eval() is disliked, as is the "Interpret" instruction > which does something similar in my usual language. "Disliked" does not quite describe it. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-10-22 14:35 +0200 |
| Message-ID | <8927386.SEqChMirdb@PointedEars.de> |
| In reply to | #7613 |
Swifty wrote:
> If I have the name of an object in a variable,
That is _not_ the name of an object (objects have _identity_, not name). It
is a possible reference path of a property (a property accessor) which value
refers to that object.
> how would I go about getting the value of that object?
>
> Example:
> varname = 'navigator.appName';
>
> How would I get the value of navigator.appName ?
eval(varname). But you really don't want to do that.
> See http://swiftys.org.uk/jsvars.html for my first, faltering attempt.
>
> The idea is to replace the multiple "document.write" lines with the
> simpler "show()" calls.
(Your current show() would still cause document.write() to be called
consecutively, which is a bad idea.)
Consider this:
var properties = [
["navigator", "appCodeName"],
["screen", "availHeight", "availWidth"],
["window", "closed", "history", "innerHeight", "innerWidth",
["location", "hostname", "pathname"], "outerHeight", "outerWidth"]
];
Then iterate recursively over this data structure, starting with
_global[properties[i][0]]
where `_global' is a reference to the ECMAScript global object (`this' in
the global execution context) and `i' is the iterator variable. Complete
and improve this:
var out = [];
for (var i = 0, len = properties.length; i < len; ++i)
{
var properties2 = properties[i];
var base = properties2[0];
for (var j = 1, len2 = properties2.length; j < len2; ++j)
{
var property = properties2[j];
out.push(
"…" + base + "." + property + "…" + _global[base][property] + "…");
}
}
document.write("…" + out.join("…") + "…");
You may use Object instances as well if you do not care about order or
if you iterate over property values in an order that you define. In any
case, you have to take care of inherited and special properties, then.
Since you are dealing with host objects here, you have to be extra careful
with "unknown"-type properties and to catch any exceptions a property access
can throw.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
[toc] | [prev] | [next] | [standalone]
| From | Swifty <steve.j.swift@gmail.com> |
|---|---|
| Date | 2011-10-22 18:59 +0100 |
| Message-ID | <3016a7h721gekekruu1hmbiqv1p0vmsd67@4ax.com> |
| In reply to | #7617 |
On Sat, 22 Oct 2011 14:35:56 +0200, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: >(Your current show() would still cause document.write() to be called >consecutively, which is a bad idea.) And there I was, congratulating myself on having very nearly written an entire page in JavaScript. One step at a time. The page is half learning exercise, half reference for properties that interest me, and which I may eventually use somewhere. -- Steve Swift http://www.swiftys.org.uk/swifty.html http://www.ringers.org.uk
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-10-22 21:22 +0200 |
| Message-ID | <1855899.XyKBKyFPRe@PointedEars.de> |
| In reply to | #7622 |
Swifty wrote: > Thomas 'PointedEars' Lahn wrote: >> (Your current show() would still cause document.write() to be called >> consecutively, which is a bad idea.) > > And there I was, congratulating myself on having very nearly written > an entire page in JavaScript. One step at a time. > > The page is half learning exercise, half reference for properties that > interest me, and which I may eventually use somewhere. If it is supposed to be a learning exercise, then you have slept right through it. In essence: 1. Avoid using eval(). Use property access syntax instead. 2. Do not call document.write() consecutively. Pass it a string value (or a value convertible to a string) that you build before. PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2011-10-22 12:53 -0700 |
| Message-ID | <78e9d1f8-c705-417a-a199-261680e5dcf4@j20g2000vby.googlegroups.com> |
| In reply to | #7613 |
Swifty wrote:
> If I have the name of an object in a variable, how would I go about
> getting the value of that object?
>
> Example:
> varname = 'navigator.appName';
>
> How would I get the value of navigator.appName ?
>
> Seehttp://swiftys.org.uk/jsvars.htmlfor my first, faltering attempt.
An approach somewhat different from Thomas', and perhaps not as safe,
is the following:
var interpret = (function() {
var global = this;
return function(str) {
var idx, obj, context = arguments[1] || global;
if (!str || typeof str != "string") return null;
idx = str.indexOf(".");
if (idx > -1) {
obj = context[str.substring(0, idx)];
return interpret(str.substring(idx + 1), obj);
} else {
return context[str];
}
}
}());
console.log(interpret("navigator.appName"));
// Or document.write, or alert, or DOM manipulation
This will not work if you have a property name that includes a ".",
and it might have problems Thomas suggested with Host objects, but for
many use cases it will work fine.
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | Lasse Reichstein Nielsen <lrn.unread@gmail.com> |
|---|---|
| Date | 2011-10-23 02:10 +0200 |
| Message-ID | <39ekeff9.fsf@gmail.com> |
| In reply to | #7613 |
Swifty <steve.j.swift@gmail.com> writes:
> If I have the name of an object in a variable, how would I go about
> getting the value of that object?
>
> Example:
> varname = 'navigator.appName';
>
So this is the reference:
globalObject["navigator"]["appName"]
So:
<script>
var global = this;
function EvalReference(name) {
var parts = name.split(/\./g);
var current = global;
for (var i = 0; i < parts.length; i++) {
current = current[parts[i]];
}
return current;
}
</script>
Then you can do:
EvalReference("navigator.appName"]
and get the expected result.
Yes, you can use eval instead. You must be certain to use global eval,
by calling it as an indirect call (not called "eval"). E.g.,
var indirectEval = eval;
var value = indirectEval("navigator.appName");
I still think it's a bad idea.
/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'
[toc] | [prev] | [next] | [standalone]
| From | Swifty <steve.j.swift@gmail.com> |
|---|---|
| Date | 2011-10-23 09:01 +0100 |
| Message-ID | <d0i7a7hduiusddc08ldrhl3l3tvc8h47fe@4ax.com> |
| In reply to | #7629 |
On Sun, 23 Oct 2011 02:10:18 +0200, Lasse Reichstein Nielsen <lrn.unread@gmail.com> wrote: >I still think it's a bad idea. Is it bad as used at http://swiftys.org.uk/jsvars.html ? It delivers the correct answers. This reminds me of my mathematics teacher, Mr Olver. If I got an answer right, but through faulty reasoning, he would say "Right answer for the wrong reason, Swift. No points". -- Steve Swift http://www.swiftys.org.uk/swifty.html http://www.ringers.org.uk
[toc] | [prev] | [next] | [standalone]
| From | Lasse Reichstein Nielsen <lrn.unread@gmail.com> |
|---|---|
| Date | 2011-10-23 11:57 +0200 |
| Message-ID | <sjmkkp2z.fsf@gmail.com> |
| In reply to | #7631 |
Swifty <steve.j.swift@gmail.com> writes: > On Sun, 23 Oct 2011 02:10:18 +0200, Lasse Reichstein Nielsen > <lrn.unread@gmail.com> wrote: > >>I still think it's a bad idea. > > Is it bad as used at http://swiftys.org.uk/jsvars.html ? That page is so simple and, probably, only really for your own usage. Performance is irrelevant. Complexity is minimal. You control all strings that are being passed to eval. By all means, use eval. The only problem is that someone might see it an think it's an acceptable method to use in real production code, where complexity is higher, you might not know which strings will end up there, and performance might matter. > It delivers the correct answers. It does. And it's simple to read. > This reminds me of my mathematics teacher, Mr Olver. If I got an > answer right, but through faulty reasoning, he would say "Right answer > for the wrong reason, Swift. No points". He had a point. Now imagine that all mathematics answers were posted on the web, and new mathamtics students would find a random solution and learn their methods from that. In that case, not marking bad solutions as bad is actually harmfull. That's where we've been, and still is, with javascript coding on the web. It's not that "eval" can't be useful, or even essential, in a few select cases *if you know what you're doing*. It's just that for each of those cases, there are thousands of "programmers" that add "eval" calls that are, at best, unnecessary, and that all have safer, faster and simpler solutions without eval. /L -- Lasse Reichstein Holst Nielsen 'Javascript frameworks is a disruptive technology'
[toc] | [prev] | [next] | [standalone]
| From | Swifty <steve.j.swift@gmail.com> |
|---|---|
| Date | 2011-10-24 14:11 +0100 |
| Message-ID | <2voaa715qn4tgua9vmmemeovv5mj61aec9@4ax.com> |
| In reply to | #7635 |
On Sun, 23 Oct 2011 11:57:24 +0200, Lasse Reichstein Nielsen <lrn.unread@gmail.com> wrote: >It's not that "eval" can't be useful, or even essential, in a few select >cases *if you know what you're doing*. It's just that for each of those >cases, there are thousands of "programmers" that add "eval" calls that >are, at best, unnecessary, and that all have safer, faster and simpler >solutions without eval. I shall evaluate my every use of eval() in future. :-) -- Steve Swift http://www.swiftys.org.uk/swifty.html http://www.ringers.org.uk
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1142@merlyn.demon.co.uk> |
|---|---|
| Date | 2011-10-23 21:16 +0100 |
| Message-ID | <AW3PMPPBYHpOFw6b@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #7613 |
In comp.lang.javascript message <a185a7pdddj3ui58j7a8rrtuivpe4gu92l@4ax.
com>, Sat, 22 Oct 2011 11:56:39, Swifty <steve.j.swift@gmail.com>
posted:
>If I have the name of an object in a variable, how would I go about
>getting the value of that object?
>
>Example:
>varname = 'navigator.appName';
>
>How would I get the value of navigator.appName ?
var Arr = varname.split(".") // gives ["navigator","appName"]
IIRC, there are only a few possibilities for Arr[0], so
var Obj = {"navigator": navigator; "document": document /*, ,,, */}
is feasible.
var Obj = {"navigator": navigator, "document": document,
"Math": Math /*, ,,, */}
var varname = 'navigator.appName';
var Arr = varname.split(".")
Result = Obj[Arr[0]][Arr[1]]
gives "Netscape", and for "Math.PI" gives about 22/7, and for
"document.writeln" gives "function writeln() { [native code]}".
Now make that a function.
To allow for inputs with other than one dot, start with Result=Obj, then
iterate through Arr (in the right direction) doing each index in turn.
Untested, therefore code not written here.
You might like <http://www.merlyn.demon.co.uk/js-props.htm>.
>The idea is to replace the multiple "document.write" lines with the
>simpler "show()" calls.
Consider
var Arr = []
Arr.push("6")
Arr.push(" fred")
document.write(A,join(""))
which is less typing per line of code.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web