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 !
  • 相关阅读:
    6_10 下落的树叶(UVa699)<二叉树的DFS>
    6_9 天平(UVa839)<二叉树的DFS>
    6_8 树(UVa548)<从中序和后序恢复二叉树>
    6_7 树的层次遍历(UVa122)<二叉树的动态创建与BFS>
    6_6 小球下落(UVa679)<完全二叉树编号>
    6_4 破损的键盘(UVa11988)<链表>
    6_3 矩阵链乘(UVa424)<用栈实现简单的表达式解析>
    6_2 铁轨(UVa514)<栈>
    第五周课程总结&试验报告(三)
    第四周课程总结和实验报告
  • 原文地址:https://www.cnblogs.com/hghrpg/p/4604170.html
Copyright © 2011-2022 走看看