突然想起的一個簡單而好玩的關於js和vbs的反射問題

今天考一個朋友的一道題
<%
  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>
一個有趣的試驗

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章