• “函数对象”的属性length


    形参的个数,Rest参数不算。

     1 function ask(question, ...handlers) {
     2   let isYes = confirm(question);
     3 
     4   for(let handler of handlers) {
     5     if (handler.length == 0) {
     6       if (isYes) handler();
     7     } else {
     8       handler(isYes);
     9     }
    10   }
    11 
    12 }
    13 
    14 // 对于积极的回答,两个 handler 都会被调用
    15 // 对于负面的回答,只有第二个 handler 被调用
    16 ask("are you a pig?", () => alert('You said yes'), result => alert(result));

    ask的参数:

    1. 一个字符串
    2. 箭头函数一号
    3. 箭头函数二号

    函数被调用时的运行过程:
    调用confirm函数,显示一个信息,根据用户选择来返回一个布尔值。

    把这个布尔值交给isYes保存

    用for...of来遍历handlers数组

    用户点击确定,isYes值为true,开始遍历handlers数组:

    先是箭头函数一号,handler.length==0返回true,因为一号确实没得参数,所以length是0.

    然后检查isYes的值,一看是true,调用箭头函数一号。

    再然后是箭头函数二号,handler.length==0返回false,因为二号有参数,所以进入else之中,调用箭头函数二号,把true传递进去。

    用户点击取消,isYes值为false,开始遍历hanlers数组:

    先是箭头函数一号,handler.length==0返回true,因为一号确实没得参数,所以length是0.

    然后检查isYes的值,一看是false,不了了之,没有后序。

    再然后是箭头函数二号,handler.length==0返回false,因为二号有参数,所以进入else之中,调用箭头函数二号,把false传递进去。

  • 相关阅读:
    Non-Photorealistic Rendering using OpenCV ( Python, C++ )
    Tensorflow Eager execution and interface
    Linear and Logistic Regression in TensorFlow
    TensorFlow Ops
    Introduction to TensorFlow
    Java Syntax Specification
    java方法的虚分派和方法表
    λ演算
    活性变量分析
    java垃圾回收机制
  • 原文地址:https://www.cnblogs.com/flyover/p/14149505.html
Copyright © 2020-2023  润新知