Interview: basics + projects + algorithms + thinking

basis

1. Determine the data type

Data types are: Value types (basic types) : String, Number, Boolean, NULL, Undefined, Symbol.

Reference data types: Object, Array, Function.

There are several ways JavaScript can determine data types: Typeof (array, [], {}); typeof (array, [], {}); typeof (array, [], [], {})

Function Fruit(name, color) {this.name = name; this.color = color; } var apple = new Fruit("apple", "red"); // (apple ! = null) apple instanceof Object // true apple instanceof Array // false

Three, use Object. The prototype. ToString. Call (), call () method can change this point, then the Object. The prototype. The toString () method to different data types above, return different results

Object.prototype.toString.call(1)
"[object Number]"

The constructor of an instance object is not available for basic data types because it automatically calls the String()/Number()/Function() constructor when using the constructor

'a'.constructor === String  //true
(1).constructor === Number  //true
(function(){}).constructor === Function  //true
apple.constructor === Fruit  //ture

Fifth, since the writing

function _typeof(obj){ var s = Object.prototype.toString.call(obj); return s.match(/\[object (.*?)\]/)[1].toLowerCase(); }; _typeof (,3,343 [12]); "array"

See: Several ways in which JavaScript determines data types