Less typing with alert

O

optimistx

When testing with alert-boxes I got tired of writing the same name twice
like this:

alert ('variablename =' + variablename);

and wrote a helper function :

var alertt = function (a) {
var result = 'Variable name and value\n';
for (var i = 0; i < arguments.length;i += 1) {
result += arguments + ' = ' + eval(arguments) + '\n';
}
alert(result);
return result;
}

// testing the above function ------------------------
var ss = 'something';
var number = 3;
var b = true;
var scope = this;

alertt('ss','number','document.lastModified','Math.random()','b','scope');



// the above code creates an alert box like this (tested in ie6.0, ff3.01,
opera9.02 only)
Variable name and value
ss = something
number = 3
document.lastModified = 09/01/2008 21:24:00
Math.random() = 0.39872433505681937
b = true
scope = [object Window]
 
G

Guest

Yes, a great idea.

One can get rid of most of the quotes at the cost of adding a split
function.

<script type="text/javascript"><!--
function alertt(a) {
arg=a.split(",")
var result = 'Variable name and value~\n'
for (var i=0; i<arg.length; i++) {
result += arg + ' = ' + eval(arg)+'~\n' }
alert(result) }

// testing the above function ------------------------
var ss = 'something ';
var number = 3;
var b = true;
var scope = this;
--></script>
<body
onload='alertt("ss,number,document.lastModified,Math.random(),b,scope")'>


I like an extra character at the end so one can see trailing blanks.

Perhaps this could be done with some sort of array and avoid the split
operation?
 
O

optimistx

Steve said:
what is the purpose of the "a" in
the following line?
var alertt = function (a) {
var result = 'Variable name and value\n';
for (var i = 0; i < arguments.length;i += 1) {
result += arguments + ' = ' + eval(arguments) + '\n';}

alert(result);
return result;

}

Thank you for looking at the code.

Variable a in argument list is unnecessary, remaining from earlier
one-variable version on of alertt:

var alertt = function (a) {
alert (a + ' = ' + eval(a) + '\n');
}

On further testing of this function there was a disappointment:
only variables in the global scope can be used, because eval()
works in global scope. Perhaps a modification to handle scope could
be found.

Also if the characters between quotes are not a variable name in global
scope then alertt behaves very badly (perhaps adding
try ...catch would help).

The growing length of alertt is not important
because it is used in testing only.
 
E

Evertjan.

optimistx wrote on 02 sep 2008 in comp.lang.javascript:
On further testing of this function there was a disappointment:
only variables in the global scope can be used, because eval()
works in global scope. Perhaps a modification to handle scope could
be found.

As usual eval is evil.

The serverside version behaves as expected.

A simplified version for a single argument,
written in ASP-js:

====================================
<script type="text/javascript">

var test = 123;

function f1() {
<%= alertt("test") %>
};

function f2() {
var test = 456;
<%= alertt("test") %>
};

</script>

<body
onload='f1();f2()'>


<script language='javascript' runat='server'>
function alertt(x){
response.write("alert('"+x+" = '+"+x+");");
};
</script>
====================================
 
D

Dan Evans

On further testing of this function there was a disappointment:
only variables in the global scope can be used, because eval()
works in global scope. Perhaps a modification to handle scope could
be found.

Also if the characters between quotes are not a variable name in global
scope then alertt behaves very badly  (perhaps adding
try ...catch would help).

The growing length of alertt is not important
because it is used in testing only.

As Evertjan said: eval is evil, and now it seems also inconvenient for
this function. I think it's better to pass the variables themselves as
opposed to a string which needs to be eval'ed. This is my attempt at
the function:

function alertt(a) {
var result = 'Variable name and value\n';
for (k in a) {
result += (k + ' = ' + a[k] + '\n');
}
alert(result);
return result;
}

It is meant to be called like:

var a = 'test';
var b = 123;
var c = true;

alertt({
a:a,
b:b,
c:c
});

To explain these line if they aren't immediately clear: the function
takes an object with the keys as the names of the variables and the
values as the values of the variables. So the "a:a" sets the key on
the left of the colon to "a" and the value on the right of the colon
to the value of "a".

I don't think a try...catch helps in the version I wrote because when
I've tested it the Reference Error occurs on the line that calls the
function. I think the trade off is that you could use the try...catch
in the original string/eval function but it has that problem with only
working on globals whereas I'm not sure how I'd use try...catch in the
function I wrote, but it should be able to understand any variables
that are available to the level of scope from which it is called.

Hope another take on the function provides some insight.
 
D

Dan Evans

I think I should add that this function is probably never going to be
as useful as firebug (an extension for the firefox browser,
http://getfirebug.com/). The reasons for this are that alert() halts
execution and so can be bad for testing things like animations where
you might want to output some values every iteration and you want to
iterate multiple times per second. (If you did want to halt execution
with firebug it provides a way to insert breakpoints and step through
execution.) I think there is a firebug lite which will provide some of
this functionality in browsers beside firefox. Finally the biggest
reason I see for using a more robust tool is that you may need to
recursively read the keys and values of nested objects. Something
like:

var foo = {
bar : {
baz : 'qux'
}
}

So far our alertt functions won't allow for nice inspection of an
object like that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top