25_underscore.js_isarraylike.html

Source: Internet
Author: User
<meta charset= "Utf-8" ><p></p><p></p><p></p><p></p><p ></p><style>*{margin:0;padding:0;} Pre{padding:10px;display:block;background-color: #eee; color:blue;font-size:20px;} </style><pre><!--today, let's explore how to make judgments an object is a class array object-->//we also need to use the last var shallowproperty = function (key) { return function (obj) {return obj = = null? void 0:obj[key];}; }; First we have to figure out what the class array is to var arr = array ();//This is the real array with the Length property Console.log (arr.length)//0// A real array object is one that can use all the methods on the Array.prototype object//But the class array also has the Length property//But they can't use all the methods on the Array.prototype//That's where the difference is. var o = {length: 2, Name: ' Ken ', age:18}//Then those are Class array objects///such as arguments in function parameters//such as DOM objects, we get through the getelementsby* and other methods of collection are class array//can use subscript operation,/ /But how can we tell if an object is a class array object//The light judging with the length attribute is not feasible,//above we said that they do not have all the methods on the Array.prototype//The value of the length property must be limited//below we come to see our und How is erscore.js implemented? var Max_array_index = Math.pow (2, 53)-1; var getlength = shallowproperty (' length '); var Isarraylike = function (collection) {//Gets the length property var length = GetLength (collection);//Determines whether the type of length is number type return typeof Lengt h = = ' number ' && length >= 0 && length <= max_array_index;}; Console.log (Isarraylike (o))//But we don't feel accurate enough.//I think we should judge the method//ourselves to implement the following var isarraylike = function (collection) {var length = GetLength (collection); Return (! ( Array.prototype.alice in collection) &&! (Array.prototype.split in collection)) && typeof length = = = ' Number ' && length>=0 && length<=max_array_index;} var oPs = Document.queryselectorall ("P") Console.log (Isarraylike (o));//trueconsole.log (Isarraylike (oPs));// Truefunction Call (A, b) {Console.log (isarraylike (arguments));//true;} Call (</pre>#), attach my github address here
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.