Summary javascript type detection

Source: Internet
Author: User
Let's talk about the blog editor first. It's too difficult to use. Once a table is pasted in, it will get stuck. Every time I use the html editor to write it, it's not good! The javascript type detection should be summarized as early as the internship, until now. At that time, there was an online bug due to this problem. Today, I first spoke about the blog garden Editor, which is too difficult to use. Once a table is pasted in, it will be stuck. Every time I use the html editor to write it, it's not good! The javascript type detection should be summarized as early as the internship, until now. At that time, there was an online bug due to this problem. Today, I will review chapter 8 "avoid empty comparison" in "Compiling maintainable javascript", which details various types of comparison problems in javascript, now I want to think that the code at that time basically made all the taboos, so I am sorry for the Party and the people when I went online... Type detection method detection object detection result disadvantages remarks Original Value typeof string number boolean value boolean undefined null object Reference Type typeof result is object, cannot detect null application ==or! = Reference value instanceof built-in type: Object, Date, Error, RegExp true/false instanceof will detect the prototype chain, each Object inherits from the user-defined type of the Object, and the function, Array true/false frame A, and B of the built-in type define the constructor Person respectively, and the two persons are identical. When an instance created in A is passed into B, frameAPersonInstance instanceof frameAPerson // trueframeAPersonInstance instanceof frameBPerson // false the only detection method: typeof function // IE8 or above object // IE8 and earlier versions for IE8 or earlier versions, since DOM is not implemented as a built-in javascript method, you need to use if ("xxx" in document) () {}; to detect the DOM method Array. isArray () Array true/false ECMAScript5, applicable to IE9 +, FF4 +, SF5 +, O10.5 +, Chrome Object. prototype. toString. call (value) = "[Object Array]" true/false It is applicable to all built-in objects, such as JSON and other attributes. If "XXX" in object property exists in the object, it will thoroughly detect the instance and its inherited object prototype hasOwnProperty () true/false // This method is not available in IE8 and later versions. // IE8 and later versions are applicable to IE8 and earlier versions. Because DOM objects are not inherited from objects, this method is not included. Therefore, you need to check whether the method exists: if (object. hasOwnProperty ("related") {}; // for non-DOM object if ("hasOwnProperty" in object & object. hasOwnProperty ("related") {}; // before calling the hasOwnProperty method of a DOM object, you must check whether the object exists! If it is known that the object is not DOM, the detection can be omitted. In general: I. Table Summary typeof is usually used for basic types (excluding null) and function detection; instanceof is usually used for custom type detection; accurate reference type detection uses Object. prototype. toString. call (value) = "[Object Array]"; in and hasOwnProperty () are used for Attribute detection (). 2. Use constructor for type detection. For more information, see http://www.cnblogs.com/zhengchuyu/archive/2008/07/21/1247764.html However, we do not recommend using regular expression match in this article. Regular expressions may affect performance, and leaving various pitfalls for you if you do not pay attention to regular expressions ~~ Iii. Performance Non-IE series: typeof>. toString. call ()> 10 * constructor IE series: typeof> constructor>. toString. call ()
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.