Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7617

Re: Get value of named object

Message-ID <8927386.SEqChMirdb@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-10-22 14:35 +0200
Subject Re: Get value of named object
Newsgroups comp.lang.javascript
Followup-To comp.lang.javascript
References <a185a7pdddj3ui58j7a8rrtuivpe4gu92l@4ax.com>

Followups directed to: comp.lang.javascript

Show all headers | View raw


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.)

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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