My blog warehouse
== vs ===, typeof vs instanceof
It is used to put some books I read and study records, welcome to like and discuss.
Strictly equal ===
No implicit conversion is performed before strict equality is compared.
Comparison steps:
- First check if it is the same type, if not, it is not strictly equal
- If they are of the same type and have the same value and are not of type number, they are congruent
- When the number type is used, both values are congruent if neither is a NaN and the values are the same or if the values are +0 and -0 respectively.
NaN === NaN // false
Copy the code
Non-strict equality ==
Type is converted to the same type before comparison, and strict equality check is performed.
There are some rules when it comes to type conversion
- Number is converted to number first when compared to string
- Number and Boolean are first converted to number when compared
- String and Boolean are converted to number first when compared
- One of them is Object which is converted to the original value first.
The whole process of judgment is
- Whether the types are the same, and if so, whether the values are the same.
- One of them is null or undefined. Null and undefined are equal only to themselves.
- One side is Boolean, which is converted to number.
- On one side, Object is converted to its original value first, and toString takes precedence.
[] = =! [] You should be able to judge correctly by now
If [] is an expression that converts to Boolean, it will be true. If [] is an expression that converts to Boolean, it will be false. False converts to the number 0, so 0===0.
List of comparison values | ||||||
---|---|---|---|---|---|---|
\ | Undefined | Null | Number | String | Boolean | Object |
Undefined | true | true | false | false | false | IsFalsy(B) |
Null | true | true | false | false | false | IsFalsy(B) |
Number | false | false | A === B | A === ToNumber(B) | A=== ToNumber(B) | A=== ToPrimitive(B) |
String | false | false | ToNumber(A) === B | A === B | ToNumber(A) === ToNumber(B) | ToPrimitive(B) == A |
Boolean | false | false | ToNumber(A) === B | ToNumber(A) === ToNumber(B) | A === B | ToNumber(A) == ToPrimitive(B) |
Object | false | false | ToPrimitive(A) == B | ToPrimitive(A) == B | ToPrimitive(A) == ToNumber(B) | A === B |
- ToNumber(A) tries to convert parameter A to A number before comparing, which has the same effect as +A (the unary operator +).
- ToPrimitive(A) converts the argument A to its Primitive value (Primitive) by trying to call A’s a.tostring () and a.valueof () methods.
typeof vs instanceof
Typeof determines the typeof the original type
// Numbers
typeof 1 === 'number'; Typeof = = = 3.14'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Although NaN is"Not-A-Number"Short for ", which means"Not a number."
typeof Number(1) === 'number'; // Don't use it like this! // Strings typeof""= = ='string';
typeof "bla"= = ='string';
typeof (typeof 1) === 'string'; // Typeof must return a String typeof String("abc") = = ='string'; // Don't use it like this! // Booleans typeoftrue= = ='boolean';
typeof false= = ='boolean';
typeof Boolean(true) = = ='boolean'; // Don't use it like this! // Symbols typeof Symbol() ==='symbol';
typeof Symbol('foo') = = ='symbol';
typeof Symbol.iterator === 'symbol';
// Undefined
typeof undefined === 'undefined';
typeof blabla === 'undefined'; // Objects typeof {a:1} ==='object'; / / using Array. IsArray or Object. The prototype. ToString. Call method can distinguish from basic Object Array type typeof [1, 2, 3] = = ='object';
typeof new Date() === 'object'; // The following is confusing, don't use it like this! typeof new Boolean(true) = = ='object';
typeof new Number(1) ==== 'object';
typeof new String("abc") = = ='object'; / / function typeoffunction() = = = {}'function';
typeof Math.sin === 'function';
Copy the code
Typeof For null date array object RegExp returns object, which is not very accurate for type comparison.
Instanceof Determines the type of the object
Grammar:
The instanceof operator tests whether the constructor's prototype property appears anywhere in the object's prototype chain. The object to be tested // must be an objectCopy the code
Instanceof is typically used to determine whether an inheritance relationship exists, and is used to test whether an object is an instanceof a particular constructor.
Implement instanceof key points:
- Return false if obj is null or not an object
- Each instance object has a private property (Proto) that points to its Prototype object, Prototype
- The prototype object also has a prototype object of its own (PROTO), cascading up until an object’s prototype object is null. Null, by definition, has no prototype and serves as the last link in the prototype chain.
function myinstanceof(obj, constructor){
if(obj === null || typeof obj ! = ='Object'){// Remove Null and non-object casesreturn false} // Get the constructor's prototype object const prototype = constructor. Prototype // Get the object's prototypelet_proto = obj.__proto__ // Object.getProtoTypeof (Object) is recommended.while(true) {if(_proTO === null){// The last linkreturn false
}
if(_proto === prototype){
return true
}
_proto = obj.__proto__
}
}
Copy the code
MDN Instanceof prototype chain MDN Instanceof prototype chain
Question:
var y = 1, x = y = typeof x;
x;
Copy the code
The expression is from right to left, x is of type undefined because the variable is promoted, so x=y=”undefined”.
(function f(f){
returntypeof f(); //"number"}) (function() {return 1; });
Copy the code
Function (){return 1; } this function. Typeof 1 returns “number” after execution through f(). So this is a very simple problem, just to distinguish between f and f().
'use strict'
var foo = {
bar: function() { console.log(this);return this.baz; },
baz: 1
};
(function() {returntypeof arguments[0](); //"undefined" function() { return window.baz; }
})(foo.bar); // function() { return this.baz; }
Copy the code
This is the environment object in which the code is currently executing. In this case, the execution environment is a self-executing function whose this refers to Arguments.
var foo = {
bar: function(){ console.log(this);returnthis.baz; }, baz: 1 } typeof (f = foo.bar)(); //undefinedCopy the code
The execution environment here is a self-executing function whose this refers to the window
var f = (function f() {return "1"; }, function g() {return2; }) (); typeof f; //"number"
Copy the code
The use of self-executing functions and grouping selectors, which pass through and separate the last value
Frequently seen exam
null == undefined // true
null === undefined // false0 = ='0'// true
false= ='0'// true[] = ='0' // false[] = =' ' // true
Copy the code
typeof null == ‘object’
Sometimes it asks you what’s wrong with the code below
if (typeof obj === 'object')
Copy the code
True if obj is null