Learning Zepto.js (prototype method) [2]

Source: Internet
Author: User

Then yesterday, continue to say the prototype method, yesterday's transmission array (yesterday, a little accident, blog post through the WP phone's UC browser after editing the label is exposed-, has been repaired);

$.grep ():

The function is similar to array.filter (in fact, the filter method of the call--)

By passing in two parameters, the first is an object of the class array, and the second is the function used to perform the judgment;

var array = [1,2,3,4,5]; var even = $.grep (array,function  (value) {      return value% 2 = == 0// - [2,4]

The function can receive three parameters passed, the top only receives one, the second is the current item in the array subscript, starting from 0--, the third is the array itself;

The value of filter is [].filter

$.inarray ():

Receive two to three parameters, the first is item (Chinese do not know how to describe, children?), the second is an array object, and the third is an optional starting subscript, which is used to check whether the first parameter exists with itself. The return value is a subscript, starting from 0.

If it is not found, it returns-1;

// -->0 // -->4

Inarray method is also directly called the array of the Idnexof method, these are ES5 new methods, in the Zepto is not to do compatible processing, but simply call method, after all, is for the mobile end of the birth;

The jquery1.x version is a compatible process, and 2.x is also the IndexOf method for direct invocation.

$.isarray ():

The method only receives a parameter, an object that is used to determine whether the object is an array, returns a Boolean value,

// -- true

This method is done with a compatible processing, if the array exists IsArray method, then call directly, otherwise the assignment is a custom function:

$.isfunction ():

Method only receives a parameter, an object, determine whether it is a function object, although this method is only used to check whether the parameter is a function, the inside derived a few variables and methods is very interesting.

Use the following:

function test1 () {} var function  //  -->true//  -->true

Put a few pieces of code on it:

The type function is first called inside the Isfunction function, and the type function returns a string

The type function uses a ternary operator to determine if it is empty, and then calls the object's ToString method, returning a string similar to the format of [object Array].

Then put in a mapping, that is Class2type (class to type), in the Zepto self-executing function through each function to an array, is the third graph that string type, loop into the Class2type object,

Now go back to the first picture, so the call should be clear.

Put a piece of class2type content

$.isplainobject ():

This method determines whether an incoming object is a pure object, or an object created through the "{}" or new object.

$.isplainobject ({}); //  .. > True$.isplainobject (new Object ()); // -- true$.isplainobject (1); // -- false

Zepto does not consider the Window object to be a purely object, but by typeof, window is indeed a object--so adding a handle to the Window object in the code

The Getprototypeof method returns the prototype of the constructor method of the parameter, first judging that the passed-in parameter is an object and is not a Window object. Interested can step in MDN

$.map ():

The function returns an array, passing in two parameters, the first is the object to traverse, can make an array of classes, or it can be an object, the second parameter is a callback function, the value returned by the callback function is stored in the collection and returned by $.map, returning an array;

function (value, key) {    ifreturn// -- []function (item, index) {    return Item * 2// - [2,4,6,8]

The method logic is not complicated, and at the last return, the called function is used to convert the passed-in class array object to an array with the following code:

The remaining two $.trim, as well as the $.parsejson, are not said, because all belong to the alias of the native function.

Today, first said here, Zepto inside of the prototype method out of the Ajax module has been said--, read the source is really the fastest way to learn.

Learning Zepto.js (prototype method) [2]

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.