Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Get value of named object

Reply
Thread Tools

Get value of named object

 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-23-2011
Swifty <> 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'

 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-23-2011
In comp.lang.javascript message <a185a7pdddj3ui58j7a8rrtuivpe4gu92l@4ax.
com>, Sat, 22 Oct 2011 11:56:39, Swifty <>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function named and called by a parameter value? Tuxedo Javascript 2 02-14-2012 10:02 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
How do I fix "Argument not specified for parameter 'value' of 'Public Overridable Sub Add(key As Object, value As Object)" Chuck Insight ASP .Net Web Controls 2 03-19-2005 12:03 AM
Re: aspx file named "object" - now Global.asax does not load Scott M. ASP .Net 0 02-05-2004 05:09 PM
Cannot access a disposed object named "System.Net.TlsStream"? Asaf ASP .Net 0 12-18-2003 01:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57