现在的位置: 首页 > 综合 > 正文

突然想起的一个简单而好玩的关于js和vbs的反射问题

2013年10月04日 ⁄ 综合 ⁄ 共 500字 ⁄ 字号 评论关闭

今天考一个朋友的一道题
<%
  function b()
    Response.write("hello world!")
  end function

  function a(str)
    //这句用str来执行b函数,请问怎么写?
  end function

  a "b"
%>

其实答案蛮简单的,用个execute(str)就解决了
在js里我自己写的是

<script>
 function b(){
    alert("hello world!");
}

  function a(str){
    eval(str+"()");
  }
 a("b");
</script>

风之石的答案是:
<script>
 function b(){
    alert("hello world!");
}

  function a(str){
    eval(str).call(0);
  }
 a("b");
</script>
用call是比较爽的办法,比我写的代码要漂亮

call 方法
调用一个对象的一个方法,以另一个对象替换当前对象。
<script>
function a(){
}
a.call(b);
function b(){
  alert("xxx");
}
</script>
一个有趣的试验

抱歉!评论已关闭.