[Effective JavaScript notes] 24th: Saving arguments objects with variables

Source: Internet
Author: User

An iterator (iterator) is an object that can sequentially access a collection of data. One of its typical APIs is the next method. The method obtains the next value in the sequence.

Iterator Example title: You want to write a handy function that can receive any number of arguments and create an iterator for those values.

Test the code well:

var it=values (1,4,1,4,2,1,3,5,6); It.next ();//1it.next ();//4it.next ();//1
Analysis: Because the values function needs to receive any number of parameters, here is the method used to construct the variable parameter function in the previous section. The iterator object inside then iterates through the elements of the arguments object. Preliminary code
function values () {    var i=0,n=arguments.length;    return {        hasnext:function () {            return i<n;        },        next:function () {            if (This.hasnext ()) {                return arguments[i++];            }            throw new Error ("reached last");}}        }    

Test with the test code above

var it=values (1,4,1,4,2,1,3,5,6); It.next ();//undefinedit.next ();//undefinedit.next ();//undefined
Error analysis

The code does not run correctly, and the initial encoding program is analyzed below.

function values () {    var i=0,n=arguments.length;//there is no error here, arguments is the built-in object in values    return {        hasnext: function () {            return i<n;        },        next:function () {            if (This.hasnext ()) {                return arguments[i++];// Error appears here, arguments is the built-in object of the next method function.            }            throw new Error ("reached last");}}        }    

The reference error here is much like another headache for this one. When you handle the point of this, you typically use variables and save the correct this. This variable is then used elsewhere. Then the solution to the arguments object comes out, with a variable to store, so that the arguments object's reference is no problem.

Encode again
function values () {    var i=0,n=arguments.length,arg=arguments;    return {        hasnext:function () {            return i<n;        },        next:function () {            if (This.hasnext ()) {                return arg[i++];            }            throw new Error ("reached last");}}        }    

Run the test code

var it=values (1,4,1,4,2,1,3,5,6); It.next ();//1it.next ();//4it.next ();//1

The results are the same as expected.

Tips
    • Beware of function nesting levels when referencing arguments

    • Binds an explicit scope reference to the arguments variable so that it can be referenced in a nested function

Appendix I: Iterators

Iterators (iterator), sometimes called cursors, are software design patterns for programming, interfaces that can be traversed on a container, and designers need not care about the contents of the container.

Iterator UML class diagram

Iterator JS Implementation

The design pattern to understand a little bit, but the specific project, there is more than the factory model, the other rarely used, the following is a simple implementation, the wrong place, welcome to communicate.
The code is as follows

function List () {    this.data=[];} list.prototype={    add:function () {        var args=[].slice.call (arguments)        this.data=this.data.concat (args) ;           },    remove:function (i) {        this.data.splice (i,1);    },    iterator:function () {        return new Iterator (this);}    } function Iterator (list) {    this.list=list;    This.cur=0;};i terator.prototype={    hasnext:function () {        return this.cur<this.list.data.length-1;    },    next : function () {        if (This.hasnext ()) {            return this.list.data[this.cur++];        }        throw new Error (' already in the end ~ ');    },    remove:function () {        this.list.remove (this.cur);}    } var list1=new List (); var it=list1.iterator (); List1.add (3,2,3,4,1,6,10,8,9); It.next ();//3it.next ();//2it.next ();// 3

[Effective JavaScript notes] 24th: Saving arguments objects with variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.