zoukankan      html  css  js  c++  java
  • 【JavaScript】函数返回值

    以下内容为学习记录,可以参考 MDN 原文。

    环境

    • vscode 1.46
    • Microsoft Edge 83

    概念

    返回值意如其名,是指函数执行完毕后返回的值。
    有些函数没有返回值就像(返回值在这种情况下被列出为空值 void 或未定义值 undefined)。

    var newString = myText.replace('string', 'sausage');
    

    返回值

    function randomNumber(number) {
      return Math.floor(Math.random()*number);
    }
    

    示例

    html 模板

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Function library example</title>
      <style>
        input {
          font-size: 2em;
          margin: 10px 1px 0;
        }
      </style>
    </head>
    <body>
    
      <input class="numberInput" type="text">
      <p></p>
    
      <script>
        const input = document.querySelector('.numberInput');
        const para = document.querySelector('p');
    
      </script>
    </body>
    </html>
    

    定义函数

    function squared(num) {
      return num * num;
    }
    
    function cubed(num) {
      return num * num * num;
    }
    
    function factorial(num) {
      var x = num;
      while (x > 1) {
        num *= x-1;
        x--;
      }
      return num;
    }
    

    定义事件

    input.onchange = function() {
      var num = input.value;
      if (isNaN(num)) {
        para.textContent = 'You need to enter a number!';
      } else {
        para.textContent = num + ' squared is ' + squared(num) + '. ' +
                           num + ' cubed is ' + cubed(num) + '. ' +
                           num + ' factorial is ' + factorial(num) + '.';
      }
    }
    
  • 相关阅读:
    Binary Stirling Numbers
    Count the Buildings ( s1 )
    P3375 【模板】KMP字符串匹配
    POJ2151Check the difficulty of problems
    fbx 模型转换 export
    Linux --windows vs
    phyreengine 3.12.0 安装遇到的问题
    A trip through the graphics pipeline 2011 Part 10(翻译)
    服务端 unity
    nsight 使用问题
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/13302215.html
Copyright © 2011-2022 走看看