Basic data types
Null, undefined, String, number, Boolean, Symbol, bigInt
Reference data type
Object, Array, Function, Data, etc
Storage differences
Basic data types are stored directly on the stack and passed by value. Reference data types are stored directly in the heap and passed by reference.
Null and undefined
- Null: indicates that a value has been defined.
(1) as an argument to a function, indicating that the argument to the function is not an object. (2) as the end of the object prototype chain.Copy the code
- Undefined: indicates that there is no definition.
(3) The object does not have an assigned attribute. The value of this attribute is undefined. (4) The function does not return a value. Default return undefinedCopy the code
- detection
null == undefined // true
null === undefined // false
typeof undefined // undefined
typeof null // object
Copy the code
Detection data type
- Typeof (array, null, object)
- Instanceof (Determines whether an instance is of a type)
console.log(bool instanceof Boolean); // false console.log(num instanceof Number); // false console.log(str instanceof String); // false console.log(und instanceof Object); // false console.log(arr instanceof Array); // true console.log(nul instanceof Object); // false console.log(obj instanceof Object); // true console.log(fun instanceof Function); // true var bool2 = new Boolean() console.log(bool2 instanceof Boolean); // true var num2 = new Number() console.log(num2 instanceof Number); // true var str2 = new String() console.log(str2 instanceof String); // trueCopy the code
- Constructor function
Object on a constructor prototype that points to the constructor itself by a constructor property. If you use constructor on an instance, you use the property directly on its constructor prototype and point to its constructor.
console.log([].constructor === Array); // true
console.log({}.constructor === Object); // true
Copy the code
- Use the Object. The prototype. The toString. Call
Call () method can change this point, the Object. The prototype. The toString will eventually return to form of the string of Object, class, the class to refer to when the detected data types.
console.log(Object.prototype.toString.call(bool)); //[object Boolean] console.log(Object.prototype.toString.call(num)); //[object Number] console.log(Object.prototype.toString.call(str)); //[object String] console.log(Object.prototype.toString.call(und)); //[object Undefined] console.log(Object.prototype.toString.call(nul)); //[object Null] console.log(Object.prototype.toString.call(arr)); //[object Array] console.log(Object.prototype.toString.call(obj)); //[object Object] console.log(Object.prototype.toString.call(fun)); //[object Function]Copy the code
- Best method of data type detection
function type(obj){ return typeof obj ! == "object" ? typeof obj :Object.prototype.toString.call(obj).slice(8,-1).toLowerCase(); }Copy the code
Typeof, Instanceof difference
- Typeof is used to determine the typeof basic data types and cannot identify object types.
- Instanceof is used to determine the type of an object, based on the inheritance relationship on the prototype chain.
Handwriting instanceof principle
How it works: As long as the right variable’s prototype is on the left variable’s prototype chain. Instanceof facilitates the prototype chain of the left variable until the prototype of the right variable is found, and returns false if the search fails.
function new_instance_of(leftVaule, rightVaule) { let rightProto = rightVaule.prototype; // Take the prototype value of the right expression leftVaule = leftvaule.__proto__; While (true) {if (leftVaule === null) {return false; } if (leftVaule === rightProto) { return true; } leftVaule = leftVaule.__proto__ } }Copy the code