如何使用 Function() 構造器定義 JavaScript 函式?


Function() 構造器期望任何數量的字串引數。最後一個引數是函式的主體——它可以包含任意 JavaScript 語句,彼此用分號分隔。

示例

你可以嘗試執行以下程式碼使用 new Function 構造器呼叫函式 −

<html>
   <head>
      <script>
         var func = new Function("x", "y", "return x*y;");

         function multiplyFunction(){
            var result;
            result = func(15,35);
            document.write ( result );
         }
      </script>
   </head>
   <body>
      <p>Click the following button to call the function</p>

      <form>
         <input type = "button" onclick = "multiplyFunction()" value = "Call Function">
      </form>
   </body>
</html>

更新日期:22-6 月 -2020

119 瀏覽量

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.