JScript 中 eval 实现 VBScript ExecuteGlobal 功能, 函数 fExecuteGlobal by shawl.qiu
说明:
顾名思义, ExecuteGlobal 就是把局部变量弄成全局变量...
虽然实现函数只是短短几行...
不过这几行中包含的原理实在是太多了, 俺不想费劲解释... 请自行消化.
shawl.qiu
2007-09-09
http://blog.csdn.net/btbtd
内容:
<%@
LANGUAGE
=
"JAVASCRIPT"
CODEPAGE
=
"65001"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template </title>
</head>
<body>
<%
fTest("j");
Response.Write("i: "+i+"<br/>");
Response.Write(typeof(i)+"<hr/>");
Response.Write("f: <xmp>"+f+"</xmp><br/>");
Response.Write("Global: "+typeof(f)+"<br/>");
Response.Write("j: <xmp>"+j+"</xmp><br/>");
Response.Write("Global: "+typeof(j)+"<br/>");
function fTest(DynArg)
{
var i=0;
fExecuteGlobal("f", f);
fExecuteGlobal("i", i);
if(DynArg) fExecuteGlobal(DynArg, eval(DynArg));
function j()
{
Response.Write("j saying hi.<br/>");
}
function f()
{
Response.Write("f saying hi.<br/>");
}
}
function fExecuteGlobal(sVar, Var)
{ // shawl.qiu code, void return
if(!sVar) throw new Error("var name must be not empty!");
eval(sVar+"=Var");
} // function fExecuteGlobal(sVar, Var)
%>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template </title>
</head>
<body>
<%
fTest("j");
Response.Write("i: "+i+"<br/>");
Response.Write(typeof(i)+"<hr/>");
Response.Write("f: <xmp>"+f+"</xmp><br/>");
Response.Write("Global: "+typeof(f)+"<br/>");
Response.Write("j: <xmp>"+j+"</xmp><br/>");
Response.Write("Global: "+typeof(j)+"<br/>");
function fTest(DynArg)
{
var i=0;
fExecuteGlobal("f", f);
fExecuteGlobal("i", i);
if(DynArg) fExecuteGlobal(DynArg, eval(DynArg));
function j()
{
Response.Write("j saying hi.<br/>");
}
function f()
{
Response.Write("f saying hi.<br/>");
}
}
function fExecuteGlobal(sVar, Var)
{ // shawl.qiu code, void return
if(!sVar) throw new Error("var name must be not empty!");
eval(sVar+"=Var");
} // function fExecuteGlobal(sVar, Var)
%>
</body>
</html>