How to detect arrays in web development

php中世界最好的语言
Release: 2018-06-04 10:48:55
Original
1258 people have browsed it

This time I will show you how to detect arrays in web development. What are theprecautions for detecting arrays in web development? The following is a practical case, let's take a look.

One of the oldest cross-domain problems in JS is passing arrays back and forth between frames. Developers quickly discovered that instanceof Array did not always return the correct results in this scenario. As mentioned above, each frame has its own Array

constructor, so instances in one frame will not be recognized in another frame. Douglas Crockford first recommended the use of the "duck typing" interface (duck typing). "Duck typing" was a concept first proposed by the writer James Whitcomb Riley, that is, "a bird that walks, swims and quacks like a duck is a duck." Essentially, focus on "what the object can do" rather than "what the object is".

// 采用鸭式辨型的方法检测数组function isArray(value) { return typeof value.sort === "function"; }
Copy after login
This detection method relies on the fact that arrays are the only objects that contain a sort() method. Of course, if the parameter passed into isArray() is an object containing the sort() method, it will also return true.

There has been a lot of research on how to detect array types in JS, and finally, Juriy Zaytsev (also known as Kangax) came up with an elegant solution.

function isArray(value) { return Object.prototype.toString.call(value) === "[object Array]"; }
Copy after login
Kangax found that calling the built-in toString() method on a value returns the standard

stringresult in all browsers. For arrays, the returned string is "[object Array]", regardless of the frame in which the array instance was constructed. The solution given by Kangax quickly became popular and was adopted by most JSclass libraries.

This method is often very useful when identifying built-in objects, but please do not use this method for custom objects. For example, using this method with the built-in JSON object will return "[object JSON]".

Since then, ECMAScript5 has officially introduced Array.isArray() into JS. The only purpose is to accurately detect whether a value is an array. Like Kangax's function, Array.isArray() can also detect values passed across frames, so many JS libraries currently implement this method similarly.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website

Otherrelated articles!

Recommended reading:

What are the event processing rules in web development

##Why do you need to avoid using global variables in web development

The above is the detailed content of How to detect arrays in web development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!