hi there,
I use this type of syntax every now and then to dynamically call functions (from incoming xml etc.):
function call_func(name,arg){ _rootname; }
function say_hi(message){ trace(message); }
call_func("say_hi","hello");
now I wanna do the same using eval:
name="say_hi"; val="hello"; eval(name+"("+val+");");
but I can't get it to work.... any ideas??? is it even possible???
interesting problem.
You may be able to get away with finding the function as an object wihtout a parameter such as this:
name="say_hi"; eval(name);
but I doubt it.
Why can't you just use the first method?
how bout
eval(name)(val);
Cool! Can you explain why that works?
eval(name) returns a reference to the function. add the parentheses and an argument and you're off.
Actually my lead programmer just explained it to me. He also said thisname; works as well.
function say_hi(message){ trace(message); }
name="say_hi"; val="hola"; eval(name)(val); // traces "hola" thisname; // traces "hola"
//edit> duh, didn't even notice that is how sakri is doing it in the first post..
yup. learned lots of neat tricks when i was running the 25 line contest. my favorite is to create or attach a movie clip and immediately call a method or change a property:
createEmptyMovieClip("thing", 0)._alpha=50;
attachMovie("thing", "thing1", 1).gotoAndPlay(100);
this works because the create/attach functions return a reference to the new clip, so the whole statement can be used as a reference to a movie clip.
cheers bit101!!! just what I was after 
and yeah... those 25 lines... I got loads out of them!!!
trabus, if you like this type of stuff... check this out:
MovieClip.prototype.draw_square=function(x,y,w,h,col){ this.lineStyle(0,0xFFFFF,0); this.beginFill(col,100); this.moveTo(x,y); this.lineTo(x,y); this.lineTo(x+w,y); this.lineTo(x+w,y+h); this.lineTo(x,y+w); this.lineTo(x,y); this.endFill(); }
XML.prototype.do_actions=function(){ var node=this.firstChild.childNodes[0]; var targ=node.attributes.target; var a=0; var b=node.childNodes.length; while(a<b){ var prop=node.childNodes[a].nodeName; if(node.childNodes[a].attributes.dtype=="int"){ var val=parseInt(node.childNodes[a].firstChild.nodeValue); }else{ var val=node.childNodes[a].firstChild.nodeValue; } _root[targ][prop]=val; a++; } }
createEmptyMovieClip("square",1); square.draw_square(0,0,100,100,0xDDCC88);
s=""; s+=""; s+= ''; s+= '<_x dtype="int">200</_x>'; s+= '<_y dtype="int">100</_y>'; s+= '<_xscale dtype="int">300</_xscale>'; s+= '<_yscale dtype="int">230</_yscale>'; s+= '<_rotation dtype="int">45</_rotation>'; s+= ''; s+=''; my_xml=new XML(s); my_xml.do_actions();
just paste it into flashMX and testmovie...
this is a "light" version... you can equally call custom functions or methods of objects like this... in theory you could construct a whole page like this... (hehe... look atwww.protosite.net
) I havent had time to work on that for ages, but the idea is that the swf loads up xml, processes it and outputs a page... a bit like xml and xslt...