|
|||||
|
|
#11 |
|
|
> 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)+'"') } > Or, something in the nature of the following framework: ALERT = eval; // Slip sheep's clothing on nasty wolf! function fmt(arg) { // Create eval-ready alert string var varList = arg.split(","); for (var k= 0, str = ""; k < varList.length; k++) { str += "+'\\n"+varList[k]+ ": '" +"+ (( typeof "+varList[k]+" == 'object') ? Dumper("+varList[k] +") : "+varList[k]+"+ ' type: '+ typeof "+varList[k]+" )"; } return "alert(" +str.substr(1)+")"; } function test(a,b) { var c = "local variable"; var d= do***ent.title; var e = { propName : "property value" }; ALERT(fmt("a,b,c,d,e")); } test("arg1",2); which extends the display to include objects. ___________ Notes: - Creating the ALERT reference to the built-in "eval" function appears to work in IE 6 and Opera 7.11. It fails in Netscape/Mozilla in that local execution context is not maintained (however, the above works if eval is used without substitution). - Dumper is an object formatting function from the following reference: <url:http://groups.google.ca/groups?q=%22matt+kruse%22++group:comp.lang.javascr ipt&hl=en&lr=&ie=UTF-8&group=comp.lang.javascript&scoring=d&selm=c4ehko 01c6%40enews2.newsguy.com&rnum=7> - It appears that the line DumperTagProperties["OPTION"] = ['text','value','defaultSelected']; needs to be included (at least currently) if the object to be formatted references an element from the DOM (as per the last line of the example given in the reference). Without it, the library code loops when executing Dumper(x);. ___________ > There is a way, I suspect, of doing it without eval. I confess to being doubtful. It would seem to me that eval is the only mechanism available that is going to allow insertion/execution of code that takes on the required context. ../rh |
|
|
#12 |
|
|
<snip> > ALERT = eval; // Slip sheep's clothing on nasty wolf! <snip> > Notes: > > - Creating the ALERT reference to the built-in "eval" function > appears to work in IE 6 and Opera 7.11. It fails in Netscape/Mozilla > in that local execution context is not maintained (however, the above > works if eval is used without substitution). <snip> Failure should be expected in that context as ECMA 262 3rd ed. Section 15.1.2.1 ends:- | If value of the eval property is used in any way other than | a direct call (that is, other than by the explicit use of | its name as an Identifier which is the MemberExpression in | a CallExpression), or if the eval property is ***igned to, | an EvalError exception may be thrown. Richard. |
|
|
#13 |
|
|
No need for eval... and, if I understand what you're trying to get at... // if title is set in html: <title>test of compoundAlert</title> // otherwise do***ent.title = "test of compoundAlert"; <script... // globals are children of window // except those specifically attached to do***ent [window.do***ent] // some stuff to test: var c = 123; var str = "this is a string"; var f = function () { var local = "test"; return local + " this"; } //I'm gonna go out on a limb here -- just for grins: function myObj() { this.prop = "property"; this.meth = function(arg) { return arg; } } var o = new myObj(); // gonna need this to grab the object constructor's name: function getConstructor(o) { var s = new String(o.constructor); // grab everything after 'function and before '(' var part = s.match(/function\s+([^\(]+)/); var name = part ? part[1].replace(/ /g,"") : "function"; // see ** note // the replace strips any unwanted spaces due to // "coding style" of: function myObj ( arglist )... return name; } /* ** note: if object is declared: myObj = function() {...} "myObj" will not be returned -- "function" will -- and the variable name will be used in compoundAlert */ function compoundAlert() { var format = ""; for(var i = 0; i < arguments.length; i++) { var argname = arguments[i]; // test for do***ent element otherwise use window var parent = argname.indexOf("do***ent.") != -1 ? do***ent : window; if(parent == do***ent) argname = argname.replace(/do***ent\./,""); var type = typeof(parent[argname]); // use ***ociative indexing var value = parent[argname]; format += "argument name: " + argname + "\n" + "argument type: " + type + "\n" + "value of argument:\n" + value + "\n\n"; // just for grins... a little lagniappe... expand objects... if(type == "object") { var tmp = parent[argname]; var cnst = getConstructor(tmp); // const is reserved keyword if(cnst == "function") // failsafe cnst = argname; format += "Object: " + cnst + "\n"; for(var a in tmp) { format += a + ": " + tmp[a] + "\n"; } format += "\n"; } } alert(format); } compoundAlert('c','str','f','do***ent.title', 'o'); // all must be p***ed as strings for varname = varvalue formatting should get you started... Things like "Math" methods, etc... are not covered here -- an exercise for you to work out (not that there's really anything useful to get -- Math methods are native code and will display '[native code]') if you have any trouble running this, let me know -- i developed it on a pc and hand-copied it over to a mac, made several revisions and patches before sending...there might be typos. Fox **************** optimistx wrote: > > 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 > embarr***ed after getting the answer. So I blush in advance . |
|
|
#14 |
|
|
Richard Cornford wrote:
> rh wrote: > <snip> > > ALERT = eval; // Slip sheep's clothing on nasty wolf! > <snip> > > Notes: > > > > - Creating the ALERT reference to the built-in "eval" function > > appears to work in IE 6 and Opera 7.11. It fails in Netscape/Mozilla > > in that local execution context is not maintained (however, the above > > works if eval is used without substitution). > <snip> > > Failure should be expected in that context as ECMA 262 3rd ed. Section > 15.1.2.1 ends:- > > | If value of the eval property is used in any way other than > | a direct call (that is, other than by the explicit use of > | its name as an Identifier which is the MemberExpression in > | a CallExpression), or if the eval property is ***igned to, > | an EvalError exception may be thrown. > But that doesn't include "may produce invalid or unintended results" as an alternative. So, as I read it, failure should be anticipated based on the possibility of an exception being thrown, but not expected. Where I would have expected an exception is on an ***ignment to eval. Interestingly though :~/, all browsers mentioned above seem to be quite willing to allow an ***ignment to eval without producing an exception. So basically, it seems your allowed to mess with eval if the browser doesn't object. And should one decide to do so, Jack*** TV is sure to deny any responsibility for the consequences. ![]() ../rh |
|
|
#15 |
|
|
There is no good solution, but this is a funny one...
var a = "asd"; alertt( a ); function alertt ( value ) { /alertt\(([^\)]*)\)/.test( alertt.caller ); alert( RegExp.$1 + " == " + value ); } "optimistx" <optimistxPoista@hotmail.com> wrote in message news:<tR6cc.1869$YQ1.796@reader1.news.jippii.net>. .. > 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 > embarr***ed after getting the answer. So I blush in advance . |
|
|
#16 |
|
|
misc@n4te.com (Nathan Sweet) wrote in message news:<4f028e5e.0404071311.4ccf3004@posting.google. com>...
> There is no good solution, but this is a funny one... > > var a = "asd"; > alertt( a ); > > function alertt ( value ) { > /alertt\(([^\)]*)\)/.test( alertt.caller ); > alert( RegExp.$1 + " == " + value ); > } > Just to tickle yourself further, you might try: function test(){ var a = "asd"; var b = "b"; alertt( b ); alertt( a ); } Math.random could also used as a way to ***ociate variable names with incorrect values. It makes debugging a real riot. ;-) Note that "caller" is deprecated as of JavaScript 1.3, and nonetheless it has a null value by definition when the call is made from the global environment ... ../rh |
|
|
#17 |
|
|
Bonjour à Nathan Sweet <misc@n4te.com> qui nous a écrit :
> There is no good solution, but this is a funny one... > > var a = "asd"; > alertt( a ); > > function alertt ( value ) { > /alertt\(([^\)]*)\)/.test( alertt.caller ); > alert( RegExp.$1 + " == " + value ); > } For "global variables" : foncAlert( s ) { alert( s +" = "+ window[ s ] ); } foncAlert( "nom_var" ); -- Cordialement, Thierry ;-) |