One, JavaScript data type
1. Basic data type (6 kinds)
-
-
- Undefined
- Null
- Boolean
- Number
- String
- Symbol (ES6 New)
2. Reference data type: Object
Second, data type detection
1, typeof can detect the basic type except NULL. Null and the typeof of all objects are "object" and cannot be used to detect user-defined types. For example, the type of date, REGEXP, Array, and DOM element are all "object".
var s = ' Hi ';
var B = true;
var num = 22;
var u;
var n = null;
var o = {};
var a = [];
var sym = Symbol ();
typeof B; Boolean
typeof Num; Number
typeof U; Undefined
typeof N; Object
typeof O; Object
typeof A; Object
typeof Sym; Symbol
2. instanceof can be used to detect reference type values. Because the values of all reference types are examples of object, the (reference type value) Instanceof object always returns True. If you use instanceof to detect the base type value, you always return false.
a instanceof Array; // true a instanceof Object; // true var fun = function () {};fun instanceof Function // true fun instanceof Object // true Span style= "COLOR: #000000" >num instanceof number // false new number (number) instanceof number // true
b instanceof Boolean//false
New Boolean (True) instanceof Boolean//true
instanceof XXX//false
U instanceof XXX//false
3, Object.prototype.toString.call () the most accurate and most common way
Object.prototype.toString.call (a);//[Object Array]Object.prototype.toString.call (o);//[Object Object]Object.prototype.toString.call (s);//[Object String]
Object.prototype.toString.call (SYM); [Object Symbol]
Object.prototype.toString.call (NewDate ());//[Object Date]
Object.prototype.toString.call (num);//[Object Number]
Object.prototype.toString.call (function() {});//[Object Function]
Object.prototype.toString.call (/abc/i); // [Object RegExp]
Object.prototype.toString.call (b); // [Object Boolean]
Object.prototype.toString.call (n); // [Object Null]
Object.prototype.toString.call (U); // [Object Undefined]
4, Jquery.type ()
5, detection array Array.isarray (a)//true
6, multi-global implementation of environmental issues. The problem with methods such as instanceof is that it is assumed to be a single global execution environment. If a Web page contains multiple frames, there are several different global execution environments, and there are several different versions of the object, array, and other constructors, and if you pass in another frame from one frame, the incoming value will have a problem with a constructor that does not pass through the values that were created natively in the second frame. such as: (http://harttle.com/2015/09/18/js-type-checking.html)
variframe = document.createelement (' iframe ');varIwindow =Iframe.contentwindow;document.body.appendchild (IFRAME); Iwindow.array= = = Array//false//equivalentIwindow.array = = = window. Array//false
So the array of Iwindow in the ARR prototype chain is not window.array. See: IWindow.document.write (' <script> var arr = [1, 2]</script> '); Iwindow.arrinstanceofArray//falseIwindow.arrinstanceofIwindow.array//true
Reference:
JavaScript Advanced Programming
http://es6.ruanyifeng.com/#docs/symbol
https://shenbao.github.io/2016/11/02/Javascript-Test-data-type/
Http://harttle.com/2015/09/18/js-type-checking.html
JavaScript data type detection