Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #7646
| From | Dr J R Stockton <reply1142@merlyn.demon.co.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Get value of named object |
| Date | 2011-10-23 21:16 +0100 |
| Organization | Home |
| Message-ID | <AW3PMPPBYHpOFw6b@invalid.uk.co.demon.merlyn.invalid> (permalink) |
| References | <a185a7pdddj3ui58j7a8rrtuivpe4gu92l@4ax.com> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar
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
csiph-web