In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the javascript type judgment example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
There are two types of data in Javascript:
Simple data types: Undefined, NULL, Boolean, Number, String
Complex data type: Object
Next let's take a look at how to do data type discrimination.
First, let's take a look at typeof.
TypeResultUndefined "undefined" Null "object" (see below) Boolean "boolean" Number "String" string "Symbol (new in ECMAScript 2015)" symbol "Host object (provided by the JS environment) Implementation-dependentFunction object (implements [[Call]] in ECMA-262 terms)" function "Any other object" object "
Have some code demo.
Let a = undefined;typeof a "undefined" let b = false;typeof b "boolean" let c = 12 position typeof c "number" let d ='12 let type of d "string" let f = function () {}; typeof f "function"
Next let's take a look at those strange phenomena.
Let str = new String ('abc'); typeof str "object" let num = new Number (12); typeof num "object" var func = new Function (); typeof func; "function" typeof null "object"
Variables created using the constructor return a "object" result using typeof judgment, with the exception of the Function function, where the variable typeof created by it returns "function"
Then let's talk about typeof null = = "object". I believe that the front-end development partners all know this result, But why? This is actually a bug that existed in the first version of javascript. For historical reasons, you can take a look at this article The history of typeof null.
About how to judge an array
Let arr = [1,2,3]; typeof arr "object"
The above result should be no stranger to everyone, so how to correctly judge the array type?
1 、 instanceof
Arr instanceof Array / / true
2 、 isArray
Array.isArray (arr) / / true
3 、 constructor.name
Arr.constructor.name / / "Array"
The third usage should be used by relatively few people, and many front-end friends have never used it. For the complex type Object, each instance of it has a constructor property.
Instanceof vs isArray
When detecting Array instances, Array.isArray is better than instanceof because Array.isArray can detect iframes.
Var iframe = document.createElement ('iframe'); document.body.appendChild (iframe); xArray = new xArray [window.frames.length-1] .Array; Array = new xArray; / Correctly checking for ArrayArray.isArray (arr); / / true// Considered harmful, because doesn't work though iframesarr instanceof Array; / / false
This code is from MDN copy. In addition to the following results, it is found that the third method constructor.name can also be correctly judged.
Arr.constructor.name / / "Array"
About NaN
Use isNaN to determine NaN.
IsNaN (1) / / true
We know that the result of NaN = = NaN is false, so how do we judge two NaN variables?
To compare the two NaN variables, use es6's Object.is ().
Let nan1 = NaNlet nan2 = NaNObject.is (nan1, nan2) true is all the content of the article "sample Analysis of Type determination in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.