|
|||||
|
|
#1 |
|
|
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 embarr***ed after getting the answer. So I blush in advance . |
|
|
#2 |
|
|
> 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) |
|
|
#3 |
|
|
"Evertjan." <exjxw.hannivoort@interxnl.net> kirjoitti viestissä news:Xns94C26883C3332eejj99@194.109.133.29... > 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','do***ent.title'); // the output is an alert box with e.g. x=3 y=abc do***ent.title=example do***ent 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? |
|
|
#4 |
|
|
optimistx wrote on 05 apr 2004 in comp.lang.javascript:
> 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 'do***ent.title', nor for '2*3*4'. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |
|
|
#5 |
|
|
optimistx wrote:
<snip> > 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 = do***ent; 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. |
|
|
#6 |
|
|
JRS: In article <tR6cc.1869$YQ1.796@reader1.news.jippii.net>, seen in
news:comp.lang.javascript, optimistx <optimistxPoista@hotmail.com> posted at Mon, 5 Apr 2004 09:17:18 : >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 p***ed. But function f(x) { alert(x+'='+eval(x)) } will do it. A general expression can be p***ed; 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. |
|
|
#7 |
|
|
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. 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) |
|
|
#8 |
|
|
Evertjan. wrote:
> 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. > 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. |
|
|
#9 |
|
|
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? |
|
|
#10 |
|
|
In article <tR6cc.1869$YQ1.796@reader1.news.jippii.net>,
"optimistx" <optimistxPoista@hotmail.com> wrote: > 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 |