Underscore.js Dependent library function Analysis II (LOOKUP)

Source: Internet
Author: User

Find:

In Underscore.js encapsulates the operation of the DOM lookup, the Find () and filter () functions, the lookup operation of the Find () function is to return the first element value that matches the condition, and the filter () function is to find all the elements that match the condition, then an array is returned. If no matching criteria are found, an empty array is returned. Next Analysis:

The Find () function:

The function finds the first element in the collection list that matches the condition based on a custom function condition in the iterator iterator, returns the first element if found, or returns "undefined".

Instance:

/**/$ (function(    ) {var  list = [1,2,3,4,5];     var first = _.find (list,function(n) {            return (! (n% 2 = = 0)) ;    });    Console.log (first);});

Run the results of the test to return the first Qualifying element 1, simply look at the example is very simple, if the analysis of the source code will find out how the designer's thinking is how to achieve this,

Source:

    //Return The first value which passes a truth test. aliased as ' detect '._.find = _.detect =function(obj, iterator, context) {//result holds the first element that can be validated        varresult; //iterate through the data with the any method and record the elements that are validatedAny (obj,function(value, index, list) {//If the result returned by the processor is converted to a Boolean type after the value is true, the current value is logged and the current element is returned            if(Iterator.call (context, value, index, list)) {result=value; return true;        }        }); returnresult; };

In the example above we give the find () function is a collection, to the source here the parameter is obj, the program to get this set directly to the function of an any of the real logic processing, see how this any exactly how to handle the lookup operation:

Source:

    //determine if at least one element in the object matches a truth test.    //Delegates to **ecmascript 5** ' s native ' some ' if available.    //aliased as ' any '.    varany = _.some = _.any =function(obj, iterator, context) {//If no processor parameters are specified, the default processor function is used and the function returns the parameter itselfIterator | | (iterator =_.identity); varresult =false; //returns a false value parameter if the obj parameter is null        if(obj = =NULL)returnresult; //preferential invocation of the some method provided by the hosting environment        if(nativesome && Obj.some = = = Nativesome)returnObj.some (iterator, context); //iterate over elements in a collectionEach (obj,function(value, index, list) {if(Result | | (Result = Iterator.call (context, value, index, list))returnBreaker;        }); //This returns a Boolean value parameter        return!!result; };

Note: The return here is worth the double exclamation "!", stating that JavaScript is a weak class of speech, the variable does not have a fixed type, so that the expression or variable before the way to add a symbol to declare the type, where the double exclamation point of the following expression or variable into a Boolean type, Like three-dimensional expressions,

See the code below for a glance:

var val =!! document.getElementById

A different way of thinking is presented:

var val  truefalse;

Underscore.js Dependent library function Analysis II (lookup)

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.