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
)
3
  • 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). Commented Nov 13, 2011 at 1:20
  • @ThiefMaster I know... I was just testing the function on a empty DOM. actually the function is able to print both [] and {} Commented Nov 13, 2011 at 1:27
  • @ThiefMaster I simplified the question, removing the whole function and only kept a fragment. Commented Nov 13, 2011 at 1:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

4 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.