Judge data type

Judge data type

data type

js has six data types, including five basic data types and one complex data type

Basic data type (value type): Number,String,Boolean,Null,undefined.

Complex data type: Object

Reference type: Object,Array,Function,Date,RegExp

Difference between value type and reference type

1. Different storage locations
The value type occupies a fixed space and is saved on the stack. The value itself is saved and copied. You can use typeOf() to detect the value type
The space occupied by the reference type is not fixed. It is saved in the heap. What is saved and copied is a pointer to the object. You need to use instanceof() to detect the data type. The object constructed by using the new() method is a reference type

2. The replication method is different
Direct assignment of value type variables is deep copy, such as var a = 10; var b = a; Then the value of a is copied to B, and B's modification of the value will not affect a. the two data are completely independent in memory.
The direct assignment of variables of reference type is actually a transfer reference, but only a shallow copy. It is to assign the stored address in the variable to a separate storage, but when one of the two variables is modified and the other reference is used for access, the modified value will also be accessed.

3. Value types cannot add properties and methods. Reference types can add properties and methods.

4. The comparison of value types is the comparison of values. They are equal only when their values are equal. The comparison of reference types is the comparison of reference addresses
When comparing, pay attention to the type conversion of the double equal sign when comparing, and the full equal sign is that only when the value and type are equal can they be equal

5. Use in functions

// Value type
var  num = 10;
function print(num) {
  num = 100;
  console.log(num);  // 100
console.log(num);   // 10
// reference type
var obj = {
    name: 'jone'
function printObj(obj) {
    obj.name = 'lucy';
    console.log(JSON.stringify(obj));  // {"name":"lucy"}
console.log(JSON.stringify(obj));  // {"name":"lucy"}

When a value type variable is modified in a function, it only takes effect within the scope of the function. When the function is destroyed, the modification will become invalid immediately; When the reference type is modified in the function, the value in the run-time data area is modified. Even if the function is destroyed, the value of the variable is still changed.

Method for judging data type


typeof null, return type error, return object

The reference type returns object except function.

Null has its own data type null, and the array, date and regular in the reference type also have their own specific types. For the processing of these types, typeof only returns the Object type at the top of its prototype chain. There is no error, but it is not the result we want.

typeof Array, Object, new Class() Will return'object', So use typeof Can not accurately judge whether the variable is object
typeof [];  //object
typeof {};  //object
typeof new (function (){});  //object
typeof 1;  //number
typeof '1';  //string
typeof null;  //object
typeof true;  //boolean


Constructor is an attribute of the prototype prototype. When a function is defined, the js engine will add the prototype prototype for the function, and the constructor attribute in the prototype points to the function reference. Therefore, rewriting the prototype will lose the original constructor.

However, this approach has problems:

1: null and undefined have no constructor, which cannot be judged by this method.

2: In addition, if you customize an object, the original constructor will be lost after the developer rewrites the prototype. Therefore, in order to standardize development, you generally need to re assign a value to the constructor when rewriting the object prototype to ensure that the type of the object instance is not tampered with.

({}).constructor === Object;  //true
(1).constructor === Number;  //true
([]).constructor === Array;  //true
('1').constructor === String;  //true
(true).constructor === Boolean;  //true
new Function().constructor == Function //true


instanceof refers to whether the object is an instance of the specified class

Instanceof is used to judge whether A is an instance of B. the expression is: A instanceof B. If A is an instance of B, it returns true; otherwise, it returns false. Special attention should be paid here: instanceof detects prototypes,

As can be seen from the above figure, the prototype of [] points to Array.prototype and indirectly to Object.prototype. Therefore, [] instanceof Array returns true and [] instanceof Object returns true.

instanceof can only be used to judge whether two objects belong to the instance relationship * *, but not the specific type of an object instance.

({}) instanceof Object;  //true
([]) instanceof Array;   //true
(1) instanceof Number;  //true
('1') instanceof String;  //true
(true) instanceof Boolean;  //true


toString() is the prototype method of the Object. Calling this method returns [[Class]] of the current Object by default. This is an internal attribute in the form of [object Xxx], where Xxx is the type of Object.

For Object objects, you can directly call toString() to return [object Object]. For other objects, you need to call call / apply to return the correct type information

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(Symbol()); //[object Symbol]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window is a reference to the global object global

Tags: Javascript Front-end ECMAScript

Posted on Wed, 01 Dec 2021 02:06:46 -0500 by spaceknop