JavaScript-Safe Type detection __java

Source: Internet
Author: User
In JavaScript, the built-in type detection mechanism is not entirely reliable. This refers to the TypeOf and Instanceof keywords respectively.
Because TypeOf has unpredictable behaviors that often result in unreliable results when detecting data types, Safari returns "function" when applying typeof to regular expressions and returns "Object" in Chrome.
instanceof There are multiple global scopes (referring to a page that contains an IFRAME), there is also a lot of problems. For example, the following code:
var IsArray = value instanceof Array;


The code above to return True,value must be an array and must also be in the same global scope as the array. If value is an array defined in another IFRAME, the code above returns false.
It can also be problematic to detect whether an object is a native object or a developer-defined object. Just like the JSON object, because many people have been using the Douglas Crockford JSON Library, which defines a global JSON object, but now the new browsers have natively supported the JSON object and do not need to rely on external libraries. It is therefore difficult to determine whether the JSON object in the page is native.
The way to solve the above problem is the same. First you should know that calling the object native tostring method on any value returns a string [Objcet XXX] format. Each class has a class attribute inside it, and in this attribute, the constructor name in the string above is developed. Let's take an example:
Console.log (Object.prototype.toString.call ([]));//[object Array] 
console.log (Object.prototype.toString.call (Window. Array));//[object function] 
console.log (Object.prototype.toString.call (object);//[object function]   
Console.log (Object.prototype.toString.call (window));//[object Global]   
console.log ( Object.prototype.toString.call (/^\d/));//[object RegExp]    


Because the name of the constructor is independent of the global scope, using ToString () ensures that a consistent value is returned. With this we can create a function that accurately determines the type of object. Like what:
function Isfunction (value) {return
        Object.prototype.toString.call (value) = "[Object Function]";
}


Note, however, that any functions implemented as COM objects in IE, Isfunction will return False (since they are not native JavaScript functions).
In web development, it is important to be able to differentiate between native and Non-native JavaScript objects. Only in this way can you know exactly what function an object has. This technique can give the right conclusion to any object, so in the future, when we need to judge the object type in the process of programming, we should discard TypeOf and instanceof.
Finally, a very important note: the Object.prototype.toString () itself can be modified, and the techniques described above apply only to unmodified native versions.
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.