首页 > 其他分享 >惰性函数(函数重写)

惰性函数(函数重写)

时间:2024-02-06 11:22:39浏览次数:25  
标签:函数 惰性 inputText input copyText document 重写

目录

惰性函数的定义

  • 惰性函数表示函数执行的分支只会在函数第一次调用的时候执行,在第一次调用过程中,该函数会被覆盖为另一个按照合适方式执行的函数,这样任何对原函数的调用就不用再经过执行的分支了。
  • 惰性函数的本质就是函数重写,所谓惰性载入,指函数执行的分支只会发生一次。

下面用在不同浏览器复制文本的案例为例,看看使用惰性函数和没有使用惰性函数的区别。

没有使用惰性函数的写法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick='copyText()'>复制文本</button>
  <input type="text">
</body>
<script>
  const inputText = document.querySelector('input')
  function copyText(){
    // 没有使用惰性函数,每次复制文本都会做一次判断
    if(navigator.clipboard){
      // 如果浏览器有clipboard API则直接在剪贴板中写入
      navigator.clipboard.writeText(inputText.value)
    }
    else{
      // 如果浏览器没有clipboard API(如IE浏览器)
      // 则创建一个input标签并复制相应文本后再销毁创建的input标签(等价于if判断的复制文本效果)
      inputText.setAttribute('value', inputText.value);
      document.body.appendChild(inputText)
      input.select()
      document.execCommand('copy')
      document.body.removeChild(input)
    }
  }
</script>
</html>

使用惰性函数的一般写法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick='copyText()'>复制文本</button>
  <input type="text">
</body>
<script>
  const inputText = document.querySelector('input')
  function copyText(){
    // 没有使用惰性函数,每次复制文本都会做一次判断
    if(navigator.clipboard){
      // 直接重写copyText函数
      copyText = () => {
        // 如果浏览器有clipboard API则直接在剪贴板中写入
        navigator.clipboard.writeText(inputText.value)
      }
      // 第二次复制文本后copyText函数已被重写
      copyText()
    }
    else{
      // 直接重写copyText函数
      copyText = () => {
     	// 如果浏览器没有clipboard API(如IE浏览器)
        // 则创建一个input标签并复制相应文本后再销毁创建的input标签(等价于if判断的复制文本效果)
        inputText.setAttribute('value', inputText.value);
        document.body.appendChild(inputText)
        input.select()
        document.execCommand('copy')
        document.body.removeChild(input)
      }
      // 第二次复制文本后copyText函数已被重写
      copyText()
    }
  }
</script>
</html>

惰性函数的高阶函数写法

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button onclick="copyText()">复制文本</button>
    <input type="text" />
  </body>
  <script>
    const inputText = document.querySelector('input')
    function createCopyText() {
      if (navigator.clipboard) {
        // 直接返回原本的copyText函数体
        return () => {
          navigator.clipboard.writeText(inputText.value)
        }
      } else {
        // 直接返回原本的copyText函数体
        return () => {
          inputText.setAttribute('value', inputText.value)
          document.body.appendChild(inputText)
          input.select()
          document.execCommand('copy')
          document.body.removeChild(input)
        }
      }
    }
    // 有需要的时候就调用createCopyText函数,可以重复使用
    // 相当于封装了copyText函数
    const copyText = createCopyText()
  </script>
</html>

标签:函数,惰性,inputText,input,copyText,document,重写
From: https://www.cnblogs.com/wjy-javascript/p/18009431

相关文章

  • 在 ​​numpy​​​ 中,​​isnan()​​​ 是用来检测数值是否为 ​​NaN​​​ 的函数
    在numpy中,isnan()是用来检测数值是否为NaN的函数¹。除了isnan(),numpy还提供了其他一些函数来处理特殊的数值,例如isinf()用来检测数值是否为无穷大,isfinite()用来检测数值是否为有限数⁶。然而,如果你想要检测的不仅仅是NaN,还包括其他类型的缺失值,例如None或者空字符串......
  • strtol() 函数
    功能概述:字符串转换为数值。其声明 位于头文件<stdlib.h>C库函数 longintstrtol(constchar*str,char**endptr,intbase) 把参数 str 所指向的字符串根据给定的 base 转换为一个长整数(类型为longint型),base必须介于2和36(包含)之间,或者是特殊值0。参数str--......
  • Fork - 进程管理中一个重要的函数
    思考问题:当首次调用新创建进程时,其入口在哪里?系统调用fork函数是如何创建进程的?系统调用exec系列函数是如何更换进程的可执行代码的?1.1进程是如何创建的?在Linux系统启动时,最早通过init_task产生的进程是idle进程,也称为swapper进程,其pid为0。该进程会调用......
  • 无涯教程-LOG10E函数
    E的以10为底的对数,约为0.434。LOG10E-语法Math.LOG10ELOG10E-示例console.log(Math.LOG10E)//thebase10logarithmofMath.E:0.434运行上面代码输出0.4342944819032518参考链接https://www.learnfk.com/es6/es6-math-property-log10e......
  • GPT 中的函数调用(function call)是什么?
    在OpenAIChatGPTAPI和GoogleGeminiAPI中我们可以看到函数调用的功能。这个功能是做什么用的?下面大概讲解。以GoogleGeminiAPI函数调用一节中的内容为例,该章节举了一个例子:大语言模型(LLMs)往往无法进行准确的数学运算。比如说,给Gemini两个数\(a\)和\(b\),让它计......
  • 无涯教程-LN10函数
    自然对数为10,约为2.302。LN10-语法Math.LN2LN10-示例console.log(Math.LN10)//thenaturallogarithmof10:~2.303运行上面代码输出2.302585092994046参考链接https://www.learnfk.com/es6/es6-math-property-ln10.html......
  • Springboot在编写CRUD时,访问对应数据函数返回null
    1.我遇到了什么问题我在学习springboot,其中在编写CRUD时发现访问数据的函数执行下去返回值是null但是其它部分正常。下面是我的错误代码pojopublicclassBot{@TableId(type=IdType.AUTO)privateIntegerid;privateIntegeruser_id;privateStr......
  • 无涯教程-LN2函数
    它返回2的自然对数,大约为0.693。LN2-语法Math.LN2LN2-示例console.log(Math.LN10)//thenaturallogarithmof10:~2.303运行上面代码输出0.6931471805599453参考链接https://www.learnfk.com/es6/es6-math-property-ln2.html......
  • 无涯教程-E函数
    这是一个欧拉常数,是自然对数的底数,大约为2.718。E-语法Math.EE-示例console.log(Math.E)//therootofthenaturallogarithm:~2.718运行上面代码输出2.718281828459045参考链接https://www.learnfk.com/es6/es6-math-property-e.html......
  • 函数的周期性的作用
    前言函数的周期性到底对研究函数有什么作用?作用列举做函数的图象由\(y=\sinx\),\(x\in[0,2\pi]\)的图像拓展到\(y=\sinx\),\(x\inR\)的图像,就是利用的函数的周期的作用。解三角不等式,常常是涉及有周期性的函数;比如求解不等式的思路:\(\sinx>\cfrac{1}{2}\),更多......