zoukankan      html  css  js  c++  java
  • Item 24: 使用变量保存 arguments 的引用

    Item 24: Use a Variable to Save a Reference to
    arguments
    An iterator is an object providing sequential access to a collection of
    data. A typical API provides a next method that provides the next
    value in the sequence. Imagine we wish to write a convenience

    function that takes an arbitrary number of arguments and builds an
    iterator for those values:
    var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6);
    it.next(); // 1
    it.next(); // 4
    it.next(); // 1
    The values function must accept any number of arguments, so we con-
    struct our iterator object to iterate over the elements of the arguments
    object:
    function values() {
    var i = 0, n = arguments.length;
    return {
    hasNext: function() {
    return i < n;
    },
    next: function() {
    if (i >= n) {
    throw new Error("end of iteration");
    }
    return arguments[i++]; // wrong arguments
    }
    };
    }
    But this code is broken, which becomes clear as soon as we attempt
    to use an iterator object:
    var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6);
    it.next(); // undefined
    it.next(); // undefined
    it.next(); // undefined
    The problem is due to the fact that a new arguments variable is implic-
    itly bound in the body of each function. The arguments object we are
    interested in is the one associated with the values function, but the
    iterator’s next method contains its own arguments variable. So when
    we return arguments[i++] , we are accessing an argument of it.next
    instead of one of the arguments of values .
    The solution is straightforward: Simply bind a new local variable in
    the scope of the arguments object we are interested in, and make sure
    that nested functions only refer to that explicitly named variable:
    function values() {
    var i = 0, n = arguments.length, a = arguments;
    return {

    hasNext: function() {
    return i < n;
    },
    next: function() {
    if (i >= n) {
    throw new Error("end of iteration");
    }
    return a[i++];
    }
    };
    }
    var it = values(1, 4, 1, 4, 2, 1, 3, 5, 6);
    it.next(); // 1
    it.next(); // 4
    it.next(); // 1
    Things to Remember
    ✦ Be aware of the function nesting level when referring to arguments .
    ✦ Bind an explicitly scoped reference to arguments in order to refer to
    it from nested functions.

    来源:Effective Javascript

    progress every day !
  • 相关阅读:
    【设计】概要设计-详细设计-到底需要输出什么???
    【Java】Eclipse代码格式化-代码模板
    【Scala】Scala学习资料
    【Java】阿里巴巴Java开发手册(纪念版)
    【Storm】学习笔记
    【HBase】学习笔记
    【Hadoop】Combiner的本质是迷你的reducer,不能随意使用
    【Hadoop】mapreduce采用多进程与spark采用多线程比较
    【ES】elasticsearch学习笔记
    【MySQL】MySQL统计NULL字段处理
  • 原文地址:https://www.cnblogs.com/hghrpg/p/4604170.html
Copyright © 2011-2022 走看看