The basis of the text of ----- typeof and instanceof

In order to consolidate the foundation, I will be described in detail by way of example, let us get to know typeof and instanceof.

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
  console.log(   'typeof 123', ':', typeof 123   ); 
  // number
  console.log(   "typeof 'str'", ':', typeof 'str'   );
  // string
  console.log(   "typeof !'0'", ':', typeof !'0'   );
  // boolean
  console.log(   "typeof new Function()", ':', typeof new Function()   );
  // function
  console.log(   'typeof myname', ':', typeof myname   );
  // undefined
  console.log(   'typeof null', ':', typeof null   );
  // object
  console.log(   "typeof {name: 'hello'}", ':', typeof {name: 'hello'}   );
  // object
  console.log(   "typeof [1,2,3]", ':', typeof [1,2,3]   );
  // object
  console.log(   "[1,2] instanceof Array", ':', [1,2] instanceof Array   );
  // true
  console.log(   "Array.isArray([1,2])", ':', Array.isArray([1,2])  );
  // true
  console.log(   "({name: 'he'}) instanceof Object", ':', ({name: 'he'}) instanceof Object   );
  // true
  console.log(   "new Date() instanceof Date", ':', new Date() instanceof Date   );
  function Person(){}
  the console.log (     new new the Person () the instanceof the Person);
   // to true 
  // Next inherit 
  function the Parent () {};
   function Child () {};
   function Other () {};
  Child.prototype = new Parent();
  let child = new Child();
  the console.log (Child the instanceof Child); // to true 
  the console.log (Child the instanceof the Parent); // to true 
  the console.log (Child the instanceof Object); // to true 
  the console.log (Child the instanceof Other); // to false 
  Console. log (Parent.prototype .__ proto__ === Object.prototype); // to true 
  // the instanceof can only be used to determine whether two objects belonging to the instance relation, but can not determine what type of a specific object instance belongs. 
  function Fun () {}
console.log(typeof fun); // function
console.log(fun instanceof Function); // true
console.log(fun instanceof Object); // true

  </script>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/sugartang/p/12461356.html