HighDots Forums  

alert('x='+x)

Javascript JavaScript language (comp.lang.javascript)


Discuss alert('x='+x) in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
optimistx
 
Posts: n/a

Default alert('x='+x) - 04-05-2004 , 02:17 AM






How to write a function f(x) or f('x') so that it produces the same output
as a call to alert-function:

var x='abc';

alert('x='+x);

In testing one needs to write similar alert() calls often. By having a
function f(), which would add the variable name in front of the variable
value one would save a lot of typing during one's lifetime.

I think there is so simple solution for this that I probable will feel very
embarrassed after getting the answer. So I blush in advance .



Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 04:16 AM






optimistx wrote on 05 apr 2004 in comp.lang.javascript:

Quote:
How to write a function f(x) or f('x') so that it produces the same
output as a call to alert-function:

var x='abc';

alert('x='+x);

In testing one needs to write similar alert() calls often. By having a
function f(), which would add the variable name in front of the
variable value one would save a lot of typing during one's lifetime.

function toAlert(x){
alert('toAlertParameter = "'+x+'"');
}

toAlert('Try me out')
toAlert( 355/113)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #3  
Old   
optimistx
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 05:19 AM




"Evertjan." <exjxw.hannivoort (AT) interxnl (DOT) net> kirjoitti viestissä
news:Xns94C26883C3332eejj99 (AT) 194 (DOT) 109.133.29...
Quote:
optimistx wrote on 05 apr 2004 in comp.lang.javascript:

How to write a function f(x) or f('x') so that it produces the same
output as a call to alert-function:

var x='abc';

alert('x='+x);

In testing one needs to write similar alert() calls often. By having a
function f(), which would add the variable name in front of the
variable value one would save a lot of typing during one's lifetime.


function toAlert(x){
alert('toAlertParameter = "'+x+'"');
}

toAlert('Try me out')
toAlert( 355/113)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Thanks Evertjan for trying. I was probably a bit unclear when asking. After
posting I could not resist trying to find a solution anyhow. One way was to
use the 'evil' eval function like this:

alertt=function(){
var s='';
for (var i=0;i<arguments.length;i=i+1){
s+=arguments[i]+'='+eval(arguments[i])+'\n';
}
alert(s);
}
// example usage:
var x=3;
var y='abc';

alertt('x','y','document.title');
// the output is an alert box with e.g.
x=3
y=abc
document.title=example document title

and
alertt('x') gives an alert box with text
x=3

Sorry to bother readers perhaps in vain. Hopefully the code is useful for
someone else except me .

By the way, is there a way to avoid eval() above?






Reply With Quote
  #4  
Old   
Evertjan.
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 06:04 AM



optimistx wrote on 05 apr 2004 in comp.lang.javascript:
Quote:
By the way, is there a way to avoid eval() above?
There is always a way to avoid evil [remember sunday school]:

toAlert=function(){
var s='';
for (var i=0;i<arguments.length;i++)
s+=arguments[i]+' = '+window[arguments[i]]+'\n';
alert(s);
}
var x=5
var y=x*x
toAlert('x','y')


this does only work for global variables,
not for 'document.title',
nor for '2*3*4'.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #5  
Old   
Richard Cornford
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 07:17 AM



optimistx wrote:
<snip>
Quote:
alertt=function(){
var s='';
for (var i=0;i<arguments.length;i=i+1){
s+=arguments[i]+'='+eval(arguments[i])+'\n';
}
alert(s);
}
snip
Sorry to bother readers perhaps in vain. Hopefully the code is useful
for someone else except me .
The code is unlikely to be of that much use to you either. You will be
able to display a number of global variable names and values along with
the property accessors and values of property accessors that use
absolute references. But you won't have much joy with local variables,
property accessors relative to the - this - keyword and many similar
conditions. Consider:-

function demonstration(s, p){
var d = document;
for(var c = 0;c < 2;c++){
alertt('c','s','p','d.title','arguments[2]');
}
}

The result would be; 'c' is probably undefined, unless there is also a
global variable called 'c' and that would not be the 'c' that was
interesting in the context of the call, 's' refers to the string that is
being built inside the - alertt - function, 'p' is like 'c' as it is a
parameter to the outer function, 'd.title' will produce an error, unless
there is a global variable 'd' that refers to an object, and
'arguments[2]' will refer to the string 'p' (the third argument to -
alertt). Nothing that might be interesting in the context of the call
to - alertt - is reported.

As an aid to development it would only really be useful in referring to
global variables and, as general programming advice is to create as few
global variables as possible, it will have limited practical
applications. It may even encourage you to adopt an inappropriate
programming style in order to make use of this tool.

Richard.




Reply With Quote
  #6  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 08:34 AM



JRS: In article <tR6cc.1869$YQ1.796 (AT) reader1 (DOT) news.jippii.net>, seen in
news:comp.lang.javascript, optimistx <optimistxPoista (AT) hotmail (DOT) com>
posted at Mon, 5 Apr 2004 09:17:18 :

Quote:
How to write a function f(x) or f('x') so that it produces the same output
as a call to alert-function:

var x='abc';

alert('x='+x);

In testing one needs to write similar alert() calls often. By having a
function f(), which would add the variable name in front of the variable
value one would save a lot of typing during one's lifetime.
It cannot be done with f(x), as the name is not then passed. But

function f(x) { alert(x+'='+eval(x)) }

will do it.

A general expression can be passed; f("3+5"), f("Math.sqrt(+X)").

Since that will not show the difference between x="" & x=" ", you may
want a different version for strings,

function s(x) { alert(x+'="'+eval(x)+'"') }

There is a way, I suspect, of doing it without eval.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #7  
Old   
Evertjan.
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 09:07 AM



Richard Cornford wrote on 05 apr 2004 in comp.lang.javascript:
Quote:
The code is unlikely to be of that much use to you either.
You are making a mistake here Richard.

Code in this NG is not only useful for execution but also,
and perhaps even more so for discussion and learning.

This code as such is perhaps totally unuseful in use, but I at least
learned that the window[x] form of global variables doesn't work if x is a
formula.

That is I did know it, otherwise it would be equally evil as eval(), but
now I is in my active memory for a while.

To construct a global variable name dynamicly it is very usefull:

var window["varNr"+n*2] = 7*n;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #8  
Old   
Richard Cornford
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 11:46 AM



Evertjan. wrote:
Quote:
Richard Cornford wrote on 05 apr 2004 in comp.lang.javascript:
The code is unlikely to be of that much use to you either.

You are making a mistake here Richard.

Code in this NG is not only useful for execution but also,
and perhaps even more so for discussion and learning.
Fair enough. I was thinking of the - alertt - function in terms of how
useful it would be for the purpose for which it was designed (alerting
name value pairs of variables, etc). Its limitations may well prove
educational.

Quote:
This code as such is perhaps totally unuseful in use, but I at least
learned that the window[x] form of global variables doesn't work if x
is a formula.

That is I did know it, otherwise it would be equally evil as eval(),
but now I is in my active memory for a while.

To construct a global variable name dynamicly it is very usefull:

var window["varNr"+n*2] = 7*n;
I would suggest that the global object is not the best repository for
dynamically created properties and their corresponding values.

Richard.




Reply With Quote
  #9  
Old   
optimistx
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 01:12 PM



Thanks to Richard Cornford for revealing the limitations of the proposed
code. It saved me a lot of testing time and possible frustration, and the
remarks about good coding style are necessary for me to remember. Evertjan's
attitude makes me feel happy, thanks .

When talking about inner functions there was an encouraging note from an
expert (was it Crockford? His home pages are a wonderful source of ideas)
that 'almost anything can be done with those'. It were a bit strange that
one could calculate the global variable value when knowing is name (as a
string), but not local. The flexibility of the Javascript language has
surprised me, but are the limits here now?



Reply With Quote
  #10  
Old   
Robert
 
Posts: n/a

Default Re: alert('x='+x) - 04-05-2004 , 03:22 PM



In article <tR6cc.1869$YQ1.796 (AT) reader1 (DOT) news.jippii.net>,
"optimistx" <optimistxPoista (AT) hotmail (DOT) com> wrote:


Quote:
In testing one needs to write similar alert() calls often. By having a
function f(), which would add the variable name in front of the variable
value one would save a lot of typing during one's lifetime.
Instead of using an alert, I write all of my debug information to a
popup window. This saves me from having to press enter for every alert.

See "Re: Debug - Your own console":
http://groups.google.com/groups?hl=e...charles-FF349F.
22451509022004%40news02.west.earthlink.net

Robert


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.