JavaScript data types to judge typeof functions

Source: Internet
Author: User
Tags cdata constructor object object

typeof methods, such as:

The code is as follows Copy Code

<strong><script type= "Text/javascript" >
<! [cdata[
var a=[0];
document.write (IsArray (a), ' <br/> ');
function IsArray (obj) {
Return (typeof obj== ' object ') &&obj.constructor==Array;
}
]]>
</script></STRONG>

However, the method does not work as we think in some browsers.

1, Firefox2 and Firefox3
In both browsers, the type of HTML object element is detected with typeof, resulting in an imprecise "function" result rather than "object" such as HTMLDocument. Such as:


Alert (typeof HTMLDocument);
In Firefox2, the result is "function";
In Firefox3, the result is "object";

2, Firefox2
For regular expressions, the result returned in the browser is "function" (the result in Firefox3 is "object"), such as:


var reg =/test/;
Alert (typeof Reg);
In Firefox2, the result is "function";
In Firefox3, the result is "object";

Note: I tested in Safari and the result is "function".
3, IE6 and IE7
Using the TypeOf method for DOM elements in IE, the result is "object". Such as:


Alert (typeof document.getElementsByTagName ("Body") [0].getattribute);
The result is "object"

4, Safari 3
Safari thinks that the nodelist of DOM elements is a function, such as:


Alert (typeof Document.body.childNodes);
The result is "function."

Obviously, if you want to test whether an object is a function, using the TypeOf method does not guarantee the test results in real sense. Then we need a solution that guarantees test results in all browsers. We know that the function itself has both the apply () and call () methods, but the two methods do not exist in IE with a problem, try the following test:


Alert (typeof document.getElementsByTagName ("Body") [0].getattribute.call)
In IE, the result is "undefined"

Obviously, we cannot take advantage of these two methods.

1.isUndefined has one more function call than the use of congruent (= = =) or typeof. It is obvious that more than one layer of function calls is less efficient (albeit trivial) than using native operators directly, but it is obvious if the number of isundefined calls is tens of thousands of times. I've added this function to the mailbox frame for more than 4,000 times, and it takes nearly 1% of the time from profiling Tools. Just one judgment account for 1% of the call time is still very scary. Of course, the isundefined in the mailbox frame is at the top of the multi-layer closure, and access to it also takes more time. If this is not enough for you to give up isundefined,

2. function is to some extent the encapsulation of some code, abstraction. is one way to organize good code and helps reduce the complexity of your code. But there is only one sentence in the Isnull/isundefined/isboolean/isnumber/isstring function, and the abstraction level is very low. Therefore, a function is extracted without encapsulation at all.

3.isUndefined (a) does not save a few bytes compared to a = = = undefined (oh, you can name shorter but lose readability).

Cases

The code is as follows Copy Code

Isnull:function (a) {
return a = = NULL;
},
Isundefined:function (a) {
return a = = = undefined;
},
Isnumber:function (a) {
return typeof A = = = ' number ';
},
Isstring:function (a) {
return typeof A = = ' string ';
},
Isboolean:function (a) {
return typeof A = = ' Boolean ';
},
Isprimitive:function (b) {
var a = typeof B;
Return!! (b = = Undefined | | | b = = NULL | | a = = ' Boolean ' | | a = = ' number ' | | = = ' string ');
},
Isarray:function (a) {
Return Proto_obj.toString.call (a) = = ' [Object Array] ';
},
Isfunction:function (a) {
Return Proto_obj.toString.call (a) = = ' [Object Function] ';
},
Isplainobject:function (o) {
if (!o | | o = = Win | | o = = DOC | | o = = doc.body) {
return false;
}
Return ' isprototypeof ' in O && proto_obj.toString.call (o) = = ' [Object Object] ';
},
Iswindow:function (o) {
Return o && typeof o = = ' object ' && ' setinterval ' in O;
},
Isemptyobject:function (o) {
For (Var a in O) {
return false;
}
return true;
}

Attached to a friend wrote

1 determining whether an array type

The code is as follows Copy Code
<script type= "Text/javascript" >
<! [cdata[
var a=[0];
document.write (IsArray (a), ' <br/> ');
function IsArray (obj) {
Return (typeof obj== ' object ') &&obj.constructor==Array;
}
]]>
</script>


2 determining whether a string type

The code is as follows Copy Code

<script type= "Text/javascript" >
<! [cdata[
document.write (isstring (' Test '), ' <br/> ');
document.write (isstring), ' <br/> ';
function isstring (str) {
Return (typeof str== ' string ') &&str.constructor==String;
}
]]>
</script>

3 Determining whether a numeric type

The code is as follows Copy Code

<script type= "Text/javascript" >
<! [cdata[
document.write (isnumber (' Test '), ' <br/> ');
document.write (Isnumber), ' <br/> ';
function Isnumber (obj) {
Return (typeof obj== ' number ') &&obj.constructor==Number;
}
]]>
</script>

4 Determine whether the date type

The code is as follows Copy Code
<script type= "Text/javascript" >
<! [cdata[
document.write (IsDate (new Date), ' <br/> ');
document.write (isDate), ' <br/> ';
function IsDate (obj) {
Return (typeof obj== ' object ') &&obj.constructor==Date;
}
]]>
</script>

5 judge whether it is a function

The code is as follows Copy Code

<script type= "Text/javascript" >
<! [cdata[
document.write (isfunction (function test () {}), ' <br/> ';
document.write (isfunction), ' <br/> ';
function Isfunction (obj) {
Return (typeof obj== ' function ') &&obj.constructor==Function;
}
]]>
</script>

6 Determine whether or not an object

  code is as follows copy code

<script Type= "Text/javascript"
LineNum
//<![ cdata[
document.write (IsObject (new Object), ' <br/> ');
document.write (IsObject), ' <br/> ');
function IsObject (obj) {
return (typeof obj== ' object ') &&obj.constructor==Object;
}
//]]>
</script>

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.