ciso
ciso

Reputation: 3050

Is there a simpler way to send variable name and content to the console?

I often need to monitor the contents of variables when testing programs like this:

var anObject = {aProperty:true}; // this is just an example.

console.log('anObject.aProperty: ' + anObject.aProperty);  <-- typed it twice.

I type the name of the variable into a string followed by typing the same thing again to reference the value.

It seems like an unnecessary duplication to write the same thing twice every time. Is there a way to do this by having to only write the name once using a function?

For example:

function show(value) {
  console.log("'" + ??? + "':" + value):
}  

so it can be used like this (or something similar):

   show(anObject.aProperty);

The above is just a simple example. Basically what I'm asking is whether there is a way to get the name of the variable that was passed into a function so that the name can then be output as part of a string showing the value of the variable.

Upvotes: 4

Views: 88

Answers (3)

ciso
ciso

Reputation: 3050

Based on coma's answer, I think this may be the solution:

function show() {
    return function (value) {
        console.log(value + ':', eval(value));
    };
};

function aTest() {
   obj = {x:1, y:2};            show()('obj');
};

function bTest() {
   obj = {x:3, y:4};            show()('obj');
};

aTest(); // obj: { x: 1, y: 2 }
bTest(); // obj: { x: 3, y: 4 }

Upvotes: 0

coma
coma

Reputation: 16649

Haters are gonna hate:

http://jsfiddle.net/coma/6HTnB/

var anObject = {
    aProperty: ['uno', 'dos', 'tres']
};

var log = function(object, property) {

    var evil = 'object.' + property;

    console.log(evil, eval(evil));
};

log(anObject, 'aProperty[2]');

Or even worse:

http://jsfiddle.net/coma/6HTnB/2/

var anObject = {
    aProperty: ['uno', 'dos', 'tres']
};

var show = function(a) {

    console.log(a + ':', eval(a));
};

show('anObject.aProperty[2]');

Well, eval is not evil per se, but the second approach is kind of ugly since the function needs to be in the correct scope.

Upvotes: 1

IQAndreas
IQAndreas

Reputation: 8468

Here is how I would write such a show() function:

function show(object, property) {
    console.log(property + ":", object[property]);
}

To use it:

var mouse = { x: 100, y: 200 };
show(mouse, 'x');

And if you want to test it: http://jsfiddle.net/IQAndreas/c9SUm/


You don't print out the name of the object (and due to the way JavaScript variables and references work, there is no easy way to do so), you only get the name of the property you want to access. If you want the name of the object as well, you could manually print it out before listing the properties:

var mouse = { x: 100, y: 200 };
console.log("== mouse ==");
show(mouse, 'x');
show(mouse, 'y');

Which outputs:

"== mouse =="
"x:" 100
"y:" 200

You can however print the type of the object, (if you are using JavaScript's class features that is, otherwise, anything created with {} is just said to be an Object):

function show(object, property) {
    var className = object.constructor.name;
    console.log(className + "#" + property + ":", object[property]);
}

Sample output (assuming you have created the Mouse class):

"Mouse#x:" 100
"Mouse#y:" 200

And if you want to test it: http://jsfiddle.net/IQAndreas/c9SUm/2/

Upvotes: 1

Related Questions