JavaScript is a weakly typed language, which is a typical example of a variable that can be assigned to any type when it is used. So let's take a look at JavaScript, which has a way of using these variable types already.
Let's take a look at the types of javascript:
String---Strings
Number---Value
Boolean---bool value
Undefined---an unspecified variable or declaration is not assigned a value
NULL---NULL
Object---Objects
Function---Functions
It is more controversial here that the function type should not be attributed to the variable type inside. For details, see
The "basic type" argument in JavaScript
These data types can be divided into 2 kinds: 1, original type (string, number, Boolean, null, undefined) 2, object (except that the original type is an object)
Now let's look at the 2 ways JavaScript creates variables: 1, object creation 2, literal creation
The first object type creates var str = new String (' Tudou '), var num = new Number (123), var bool = new Boolean (true), Console.log (typeof str, t ypeof num, bool);//object object boolean//the second literal way str = ' Tudou '; num = 123;bool = True;console.log (typeof str, typeof num, BOOL);
The first is to create the data from a class that corresponds to a new one, so that you get an object of the corresponding data type. Let's look at the breakpoint in front of the first Console.log ():
PS: You can create changes by passing different data types directly through New Object (), and JavaScript automatically determines what data type you need to create by passing in the values you pass in.
Look at the red boxes where they are objects and inherit their respective data types.
The second is to create the data by means of a literal, which describes the representation of a fixed value in the code. This gets the direct amount of a data:
You can see that the data is purely data, nothing else. That is, the above STR itself cannot invoke any function because it is a "data" instead of an object, which means that the operation of the string is possible in such a way:
var str = ' abc '; strstr (str, ' a ');
This kind of operation is a bit cumbersome and troublesome, not convenient. So JavaScript introduces the wrapper type.
We often write this in javascript:
var str = ' abc '; Str.indexof (' a ')
So how does it do that pinch? When we execute the Str.indexof () method, JavaScript does the following conversion
1. Create an object instance of type String
2. Call the IndexOf () method on an instance of an object
3. Destroy this object instance
The code might look like this:
var str1 = new String (' abc '), var str2 = Str.indexof (' a '); str1 = Null;return str2;
The processing of some of these columns allows us to use the literal creation of the variable to invoke the object's method, greatly increasing the convenience of the operation.
Here are some basic types of JavaScript variables, 2 ways to create variables, and wrapper types now look at how we determine what a variable is in JavaScript.
First look at the following code:
var str = "a"; var num = 1;var bool = True;var arr = [];var obj = {};var Undef;var nul = Null;log (typeof str, typeof num, t ypeof bool, typeof obj, typeof arr, typeof Nudef, typeof nul), arr = new Array (n/a), obj = new Object (), str = new String (' A '); num = new number (1); bool = new Boolean (true); log ("<br>"); log (typeof str, typeof num, typeof bool, typeof obj, Ty Peof arr, typeof Nudef, typeof nul), function log () {for (var i=0; i<arguments.length; i++) {document.write (Arguments[i] + "\ T");}}
Its output data is:
String number Boolean Object object undefined object
Objectobjectobjectobjectobjectundefinedobject
The typeof operator detects a variable, so long as the variable is a reference type, it returns object so when you create a base database type using the New keyword, typeof returns all object, so you can't use typeof to detect the data type. Then there's no other way.
JavaScript also provides a way to detect an instance of an object instanceof maybe, probably, you can try it.
var str = "a"; var num = 1;var bool = True;var arr = [];var obj = {};var Undef;var nul = Null;log (str instanceof String, Nu M instanceof number, bool instanceof Boolean, obj instanceof object), arr = new Array (n/a); obj = new Object (); str = new S Tring (' a '); num = new number (1); bool = new Boolean (true); log ("<br>"); log (str instanceof String, num instanceof numbe R, bool instanceof Boolean, obj instanceof Object), function log () {for (var i=0; i<arguments.length; i++) { document.write (arguments[i]+ "\ t");}}
Its output is:
False False True
Truetruetruetrue
The instanceof is used to identify the type of object being processed. Unlike the TypeOf method, the Instanceof method requires the developer to explicitly confirm that the object is of a particular type. This can only detect if the variable is an instance of an object, nor does it meet our needs.
If you want to implement type detection in JavaScript, you need to use Object.prototype.toString.call (str);
var str = ' a '; var str1 = new String (' a '); Console.log (Object.prototype.toString.call (str), Object.prototype.toString.call (STR1));
Its output is:
[Object String] [Object String] they are consistent. Then we can determine the object type based on this.
We can do simple processing of them to expose them to the global window, so that we could use them later.
var typelist = ' String number Boolean Array Object Date Function RegExp '. Split ('), toString = Object.prototype.tostring;f or (var i=0; i<typelist.length; i++) {(function (index) {window[' is ' +typelist[index]] = function (obj) {var reg = new REGEXP (Typelist[index]) return reg.test (Tostring.call (obj));}) (i);} var str = ' abc '; var num = 1;var bool = True;var obj = {};var date = new Date (); var arr = [];var reg =/abc/;console.log (is String (str), isnumber (num), Isboolean (BOOL), IsObject (obj), isDate (date), IsArray (arr), Isregexp (reg));// Trueconsole.log (IsObject (num));//false
The above is a summary of the individual, the wrong place please point out thank you.
Talk about JavaScript variable type and variable type detection