Js instanceof judgment problem

problem description

Type judgment

the environmental background of the problems and what methods you have tried

Type judgment

related codes

a = true
b = new Boolean(true)
b instanceof Boolean // true
a.constructor === b.constructor // true

why?
a instanceof Boolean ? false

what result do you expect? What is the error message actually seen?

can you analyze the difference between a = true and new Boolean (true), the principle of instanceof judgment

Aug.17,2021

  • enter typeof an in the console and you will find that it is a boolean' rather than a Boolean
  • then type typeof b and you will find that the output is an 'Boolean in object' js is just an object
  • instanceof determines whether the prototype object of the object on the left is the sibling prototype. of the constructor on the right. When judging an instanceof Boolean, the bottom layer of js converts an into an object with attributes. In fact, they are the same at the bottom, but it is not from new, so instanceof returns false
  • .
  • imagine that you customize a class and generate an instance of that class in other ways when you don't call it through new. Does the program become a little unpredictable?
  • the above is only a personal humble opinion, not an authoritative answer. When the great god sees Mo Pen, we can communicate and learn from each other.

first explain that the instanceof,
instanceof operator is used to detect whether constructor.prototype exists on the prototype chain of the parameter object.

An instanceof B / / detects whether the prototype of constructor B appears on the prototype chain of object A.
so the simple data type you create with this declaration: a = true, there is no way to use instanceof. Because it is not an object instance.

for a.constructor = b.constructor / / true
there is an automatic boxing operation inside a.constructor actually js. Otherwise, the simple data type itself does not contain properties and methods.
recall how often we write code like var a = '123; if (a.length > 0).

about the type judgment in js, please take a look at my summary https://codeshelper.com/a/11.

.
a = true
b = new Boolean(true)
b instanceof Boolean   //true  bBooleanba;
The

instanceof operator can only be used on objects, but not on primitive values
secondly, a has constructor, because console.log (new Boolean (a) .constructor);

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e34009-4427e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e34009-4427e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?