In the last article, we looked at the concept of stereotypes and stereotype chains in detail, and elaborated on the “metaphysical” relationship between stereotype chains through a classic question. In this article, we will look at the data type judgment methods

typeof

What types of data can typeOF detect

  • “undefined”
  • “boolean”
  • “string”
  • “number”
  • “object”
  • “function”

Why does typeof NULL return “object”?

Null is considered a reference to an empty object, whose data type is Object

The principle of

At this point, we should consider how JavaScript stores data, or what are the trade-offs of data types for a variable?

In the initial version of javascript, the 32-bit system was used to store the type information of variables in the low order for performance:

  • 000: object
  • 010: floating point number
  • 100: string
  • 110: Boolean
  • 1: the integer

In the case of undefined and NULL, however, the information storage for these two values is a bit special.

Null: The null pointer corresponding to the machine code, usually all zeros.

Undefined: an integer of −2^30!

As a result, Typeof has a problem judging null, because null is treated as an object because all machine code is zero.

instanceof

The principle of

  • Instanceof is an instance used to determine whether A is B, expressed as: A instanceof B.
  • Instanceof can only be used to determine whether two objects belong to the instance relationship, but not the specific type of an object instance
  • Instanceof support for reference types is good, but it can’t judge primitive types, so instanceof is used only when Typeof is judged to be object.
console.log([] instanceof Array); //true console.log({} instanceof Object); //true console.log(new Date() instanceof Date); //true console.log([] instanceof Object); //true console.log(new Date() instanceof Object); //true console.log("123" instanceof String); //false // We can see from the above that all Object instances can refer to the Object class indirectly due to the original necklace.Copy the code

Handwritten implementation

function my_instance_of(leftVaule, rightVaule) {
if((typeof leftValue !== 'object') || leftValue === null) return false;
let proto = leftValue.__proto__;
while(true){
if(proto === null) return false;
if(proto === rightValue.prototype) return true;
proto = proto.__proto__;
}
}
Copy the code

constructor

Constructor can print the class to which the instance belongs as: instance. Constructor So the way to identify each type is:

console.log([].constructor == Array);       //true
console.log({}.constructor == Object);      //true
console.log("string".constructor == String);        //true
console.log((123).constructor == Number);       //true
console.log(true.constructor == Boolean);       //true
console.log([].constructor == Object)       // false    
Copy the code

Object.prototype.toString.call()

ToString () is a prototype method for Object that, by default, returns the current Object’s [[Class]]. This is an internal property of the form [object Xxx], where Xxx is the type of the object.

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

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

The principle and realization of new keyword

  • The new operator creates an instance of a user-defined object type or of a built-in object with a constructor. The new keyword does the following:
1. Create an empty simple JavaScript object ({}); 2. Link this object (that is, set its constructor) to another object; 3. Use the object created in Step 1 as the context for this. 4. If the function does not return an object, return this.Copy the code
  • Look at the following examples:
Function Person() {this.name = 'xy'} Create an empty object: var obj = {} 2. Assign an empty object to this: this = obj 3. Prototype :this.__proto__ = Person().prototype 4. Return this: return thisCopy the code

Implementation code 1

function newOper(){
  let obj = {}
  let constructor = Array.prototype.shift.apply(arguments)
  obj.__proto__ = constructor.prototype
  var ret = constructor.apply(obj, arguments)
  return typeof ret === 'object' ? ret : obj
}
Copy the code

Implementation Code 2

function newOper(cons, ... args){ if(typeof cons ! == 'function'){ throw 'the first param must be a function' } let obj = Object.create(cons.prototype) let res = cons.apply(obj, args) let isObject = typeof res === 'Object' && res ! == null let isFunction = typeof res === 'function' return isObject || isFunction ? res : objCopy the code