zoukankan      html  css  js  c++  java
  • Use apply to Call Functions with Different

    Item 21: Use apply to Call Functions with Different
    Numbers of Arguments
    Imagine that someone provides us with a function that calculates the
    average of any number of values:

    average(1, 2, 3); // 2
    average(1); // 1
    average(3, 1, 4, 1, 5, 9, 2, 6, 5); // 4
    average(2, 7, 1, 8, 2, 8, 1, 8); // 4.625

    The average function is an example of what’s known as a variadic or
    variable-arity function (the arity of a function is the number of argu-
    ments it expects): It can take any number of arguments. By com-
    parison, a fixed-arity version of average would probably take a single
    argument containing an array of values:

    1 averageOfArray([1, 2, 3]); // 2
    2 averageOfArray([1]); // 1
    3 averageOfArray([3, 1, 4, 1, 5, 9, 2, 6, 5]); // 4
    4 averageOfArray([2, 7, 1, 8, 2, 8, 1, 8]); // 4.625

    The variadic version is more concise and arguably more elegant. Vari-
    adic functions have convenient syntax, at least when the caller knows
    ahead of time exactly how many arguments to provide, as in the
    examples above. But imagine that we have an array of values:

    var scores = getAllScores();

    How can we use the average function to compute their average?
    average(/* ? */);
    Fortunately, functions come with a built-in apply method, which is
    similar to their call method, but designed just for this purpose. The
    apply method takes an array of arguments and calls the function as
    if each element of the array were an individual argument of the call.
    In addition to the array of arguments, the apply method takes a first
    argument that specifies the binding of this for the function being
    called. Since the average function does not refer to this , we can sim-
    ply pass it null :

    var scores = getAllScores();
    average.apply(null, scores);

    If scores turns out to have, say, three elements, this will behave the
    same as if we had written:

    average(scores[0], scores[1], scores[2]);

    The apply method can be used on variadic methods, too. For example,
    a buffer object might contain a variadic append method for adding
    entries to its internal state (see Item 22 to understand the implemen-
    tation of append ):

    var buffer = {
      state: [],   append:
    function() {     for (var i = 0, n = arguments.length; i < n; i++) {       this.state.push(arguments[i]);     }   } };

    The append method can be called with any number of arguments:

    buffer.append("Hello, ");
    buffer.append(firstName, " ", lastName, "!");
    buffer.append(newline);

    With the this argument of apply , we can also call append with a com-
    puted array:

    buffer.append.apply(buffer, getInputStrings());

    Notice the importance of the buffer argument: If we passed a dif-
    ferent object, the append method would attempt to modify the state
    property of the wrong object.
    Things to Remember
    ✦ Use the apply method to call variadic functions with a computed
    array of arguments.
    ✦ Use the first argument of apply to provide a receiver for variadic
    methods.

     文章来源于:Effective+Javascript编写高质量JavaScript代码的68个有效方法 英文版

    progress every day !
  • 相关阅读:
    SQL: 返回刚插入记录的ID 及 output 更新记录时得到更新记录的ID值 .
    asp.net 用伪静态修改webconfig配置文件 .
    Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
    js闭包(转载) (jquery ajax 异步 多循环运用 )
    c# enum 枚举名称和ID 互换获取.................
    WCF :服务开发与调用的完整示例
    关于DataSet 、 DataTable 、 ArrayList 、 IList 、Array
    Expression Tree不完全入门
    WF4 Beta2:IExecutionProperty的使用
    Groovy 1.8.9, 2.0.7 和 2.1.1 发布
  • 原文地址:https://www.cnblogs.com/hghrpg/p/4604036.html
Copyright © 2011-2022 走看看