java script eval c_javascript当中eval用法

1)eval

例 4.1.1

/*马克-to-win:var scriptCode = "c = a * b";

var a = 5;

var b = 10;

var c = 2;

eval(scriptCode);

以上的话就相当于:

eval("c = a * b");===c = a * b

eval是global的方法,

*/

var result = window.eval("1+2/4") ;//根据上面所说,result=1+2/4;

document.write(result)

var s="today=new Date();document.write(today.toLocaleString())"

eval(s)

//-->

例 4.1.2

在eclipse中直接open with火狐即可

//例1

var s = "2+31-18";

/*When the eval() function is called, it expects a string to be

passed to it as its single argument value. The contents of that

string should be syntactically correct executable script source

text.*/

document.write(eval(s));

var s1 = "2+31a-18";

/* note that we must comment out the following statement,

otherwise, it reports error.*/

//document.write(eval(s1));

//例2

eval("d =new Date();document.write(d.toLocaleString())")

//eval()函数的参数为字符串,功能是将该字符串执行出来。体会“执行”的意思!

//-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值