在另一个脚本上使用return语句调用JavaScript函数

问题描述 投票:-5回答:2
<body>      
<script>;

      function myfunction() { 
        num1 = 8
        num2 = 25
        num3 = 10


        return (num1 * num2) + num3)
    }
    document.write(myfunction()) 
    </script>

    <script>
    myfunction() 
    </script>
</body>

我目前正在学习JavaScript,我在另一个脚本上调用该函数时遇到问题。

javascript function
2个回答
0
投票

你的代码很好,你错过了一个括号,你用过:

return (num1 * num2) + num3) 

应该:

return ((num1 * num2) + num3)

-1
投票

我觉得你有麻烦,因为你有一个;在你的脚本的开头,它错过了(在你的回报中。

<body>      
<script>

      function myfunction() { 
        num1 = 8
        num2 = 25
        num3 = 10

        //document.write((num1 * num2) + num3) 
        return ((num1 * num2) + num3)
    }
    //document.write(myfunction()) //this code works with document.write
    </script>

    <script>
    var myres = myfunction() //this code only works with document.write
    </script>
</body>

为我工作!

© www.soinside.com 2019 - 2024. All rights reserved.