eval方法

eval() 把括号内的字符串当作标准语句或表达式来运行。

eval 方法

检查 JScript 代码并执行.

eval(codeString)

必选项 codestring 参数是包含有效 JScript 代码的字符串值。这个字符串将由 JScript 分析器进行分析和执行。

说明

eval 函数允许 JScript 源代码的动态执行。例如,下面的代码创建了一个包含 Date 对象的新变量 mydate

eval("var mydate = new Date();");

传递给 eval 方法的代码执行时的上下文和调用 eval 方法的一样.

 

 

JS中的eval和document.getElementById

eval和document.getElementById都能把字符串转化为对应的对象或是数值,但两者之间还是有区别的,请看以下几个例子:
假如页面上存在ID为aaa的标记,以下a和b得到的是一样的结果

var a=eval('aaa'); 
var b=document.getElementById('aaa'); 
var a=eval('aaa');
var b=document.getElementById('aaa');

如果页面上不存在aaa标记,则eval将发生错误,document.getElementById返回null值

另一种情况就是字符串为表达式时

alert(eval('1+1')); //返回2 
alert(document.getElementById('1+1')); //返回null因为getElementById会把'1+1'看作 
//
标记ID进行查找 
alert(eval('aaa.href')); //将返回aaa的href属性 
alert(document.getElementById('aaa.href')); //和上面一样会返回null

 

JS中的eval和falsh中的eval

在 ActionScript 3.0 中,取消了对 eval() 函数的支持,因此,如果试图使用该函数,则会引发错误。

Flash Player 的先前版本使用 eval() 函数来按照名称访问变量、属性、对象或影片剪辑。

 

flash eval 函数

eval(expression:Object) : Objecteval(expression:String) : Object

按名称访问变量、属性、对象或影片剪辑。如果表达式是变量或属性,则返回该变量或属性的值。如果表达式是对象或影片剪辑,则返回对该对象或影片剪辑的引用。如果找不到表达式中指定的元素,则返回 undefined。

 

在 Flash 5 或更高版本中,不能使用 eval() 动态设置和检索变量值或实例名称,因为不能在等式的左侧使用 eval()。例如,将代码

eval ("var" + i) = "first";

替换为:

this["var"+i] = "first"

或者替换为:

set ("var" + i, "first");

 

下面的示例使用 eval()动态命名的影片剪辑设置属性。此 ActionScript 为三个影片剪辑(分别称为 square1_mcsquare2_mcsquare3_mc)设置 _rotation 属性。

for (var i = 1; i <= 3; i++) {
setProperty(eval("square"+i+"_mc"), _rotation, 5);
}

也可以使用以下 ActionScript:

for (var i = 1; i <= 3; i++) {
this["square"+i+"_mc"]._rotation = -5;
}
//对象后面加方括号:thsi[],mc[]的形式,表示了对象所属的成员,但不能是成员中的成员。
//说白了,就是对象成员数组。
//方括号中只能是对象“直辖”的成员名
//方括号可以放在路径的中间或后面,不能把方括号放在最前面。

//eval();是计算表达式。可作为路径的开头,不能放在中间或后面。

//场景上有个影mc1,mc1里面放有一个mc2,测试如下:

trace("----访问mc1的测试-----")
trace(["mc1"]);//mc1
trace([mc1]);//_level0.mc1
trace(this["mc1"]);//_level0.mc1
trace(this[mc1]);//_level0
trace(this["_root.mc1"]);//undefined
trace(eval("mc1"));//_level0.mc1
trace(eval(mc1));//_level0.mc1
trace(eval("_root.mc1"));//_level0.mc1

trace("----访问mc2测试---")
trace(this[mc2]);//undefined
trace(this["mc2"]);//undefined
trace(this["mc1.mc2"]);//undefined
trace(this["_root.mc1.mc2"]);//undefined
trace(eval(mc2));//undefined
trace(eval("mc2"));//undefined
trace(eval("mc1.mc2"));//_level0.mc1.mc2
trace(eval("_root.mc1.mc2"));//_level0.mc1.mc2

trace("-----路径与属性访问-----")
trace(this["mc1.mc2._x"]);//undefined
trace(this.mc1["mc2._x"]);//undefined
trace(this.mc1.mc2["_x"]);//124.95
trace(mc1["mc2._x"]);//undefined
trace(mc1.mc2["_x"]);//124.95
trace(eval("mc1._x"));//-12.95
trace(eval("mc1.mc2._x"));//124.95
trace(mc1.eval("mc2._x"));//undefined
trace(mc1.mc2.eval("_x"));//undefined

trace("---访问mc2的实例名---")
trace(mc1["mc2._name"]);//undefined
trace(mc1["mc2"]._name);//mc2
trace(mc1.mc2["_name"]);//mc2
trace(["_name"]);//_name
trace(["mc1"]._name);//undefined
trace(mc1.eval("mc2._name"));//undefined
trace(mc1.eval("mc2")._name);//undefined
trace(mc1.mc2.eval("_name"));//undefined
trace(eval("mc1.mc2._name"));//mc2
trace(eval("mc1.mc2")._name);//mc2
trace(eval("mc1").mc2._name);//mc2
posted @ 2008-11-27 11:18  爱恋永恒  阅读(846)  评论(0)    收藏  举报