0

I'm created a function to print a object or an array, this print the properties name and values ok, but I can't figure out how can I print the "testobject" intead of "object" on the first line of the output.

part of the function

function expandNode(obj) {
    for (var node in obj) {
        text += node + " => " + obj[node] + "<br>";  //keyword => value
    }
    return (text);
}

exemple

var testobject={};
testobject["car"]="toyota";
testobject["instrument"]="piano";
testobject["computer"]="macintosh";
document.write( printO(testobject) );

the output is

object
(
    car => toyota
    instrument => piano
    computer => macintosh
)
Vitim.us
  • 20,746
  • 15
  • 92
  • 109
  • 2
    Fyi, you should never use `new Array()` but `[]` instead. Additionally, using `document.write()` is bad as it will erase everything in your document if you use it after the DOM is fully loaded. Additionally you need to make `myarray` an object, using `{}` instead of `new Array()`, since arrays only have keys in the interval `[0, length)`. – ThiefMaster Nov 13 '11 at 01:20
  • @ThiefMaster I know... I was just testing the function on a empty DOM. actually the function is able to print both [] and {} – Vitim.us Nov 13 '11 at 01:27
  • @ThiefMaster I simplified the question, removing the whole function and only kept a fragment. – Vitim.us Nov 13 '11 at 01:57

2 Answers2

1

It is impossible. The only thing you can do is passing the variable name, too:

printO('someVar', someVar);

No need to say that this is extremely ugly so better don't do it at all.


Since another answer suggested to pass only the name and use eval to access the value: This would work for globals - but in that case the better way would be not using eval but the [] operator on the global object (window or this). Since it's impossible to access a value from a local scope though there is no good way to make your function accept only the variable name.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • I thought it, but I think I will only print the "parent" type, `object(` or `array(`. it will be more elegant solution, I guess... but thanks anyway – Vitim.us Nov 13 '11 at 02:04
0

Unfortunately this isn't possible in Javascript. The best you could do would be to pass in the variable name as a string and then use eval() to access the actual variable:

function printO(obj_str) {
  alert("The variable name is " + obj_str);
   obj = eval(obj_str);
}

See also this question: How to convert variable name to string in JavaScript?

Edit: In fact, the above solution won't work; see discussion below.

Community
  • 1
  • 1
Alex Peattie
  • 26,633
  • 5
  • 50
  • 52
  • Eval is evil and it would only work with global variables. And then it wouldn't be necessary at all since he could use `this[obj_str]` or `window[obj_str]` to access it. But there'd be absolutely no way to access a variable from a local context. – ThiefMaster Nov 13 '11 at 01:38
  • I don't disagree that `eval` is evil, but I thought, as `eval` runs in the same scope as the caller, it would still work. But I'm guessing I'm mistaken. – Alex Peattie Nov 13 '11 at 01:44
  • I'm using the same methodology to open children, but I can't get the variable name outside a parent. But I can't get the point of your code. – Vitim.us Nov 13 '11 at 01:44
  • Yes, `eval` does run in the scope of the caller - and the caller is `printO`, so unless you actually define the *function* `printO` in the context where the variable you want to access is available it won't work. – ThiefMaster Nov 13 '11 at 01:46