javascript中typeof和instanceof
Posted _linka
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript中typeof和instanceof相关的知识,希望对你有一定的参考价值。
typeof:返回值是一个字符串,一般只能返回以下结果
number、string、object、boolean、undefined、function、symbol
Type | Result |
---|---|
Undefined | "undefined" |
Null | "object" (see below) |
Boolean | "boolean" |
Number | "number" |
String | "string" |
Symbol (new in ECMAScript 2015) | "symbol" |
Host object (provided by the JS environment) | Implementation-dependent |
Function object (implements [[Call]] in ECMA-262 terms) | "function" |
Any other object | "object" |
1 // Numbers 2 typeof 37 === \'number\'; 3 typeof 3.14 === \'number\'; 4 typeof(42) === \'number\'; 5 typeof Math.LN2 === \'number\'; 6 typeof Infinity === \'number\'; 7 typeof NaN === \'number\'; // Despite being "Not-A-Number" 8 typeof Number(1) === \'number\'; // but never use this form! 9 10 11 // Strings 12 typeof \'\' === \'string\'; 13 typeof \'bla\' === \'string\'; 14 typeof \'1\' === \'string\'; // note that a number within a string is still typeof string 15 typeof (typeof 1) === \'string\'; // typeof always returns a string 16 typeof String(\'abc\') === \'string\'; // but never use this form! 17 18 19 // Booleans 20 typeof true === \'boolean\'; 21 typeof false === \'boolean\'; 22 typeof Boolean(true) === \'boolean\'; // but never use this form! 23 24 25 // Symbols 26 typeof Symbol() === \'symbol\' 27 typeof Symbol(\'foo\') === \'symbol\' 28 typeof Symbol.iterator === \'symbol\' 29 30 31 // Undefined 32 typeof undefined === \'undefined\'; 33 typeof declaredButUndefinedVariable === \'undefined\'; 34 typeof undeclaredVariable === \'undefined\'; 35 36 37 // Objects 38 typeof {a: 1} === \'object\'; 39 40 // use Array.isArray or Object.prototype.toString.call 41 // to differentiate regular objects from arrays 42 typeof [1, 2, 4] === \'object\'; 43 44 typeof new Date() === \'object\'; 45 46 47 // The following is confusing. Don\'t use! 48 typeof new Boolean(true) === \'object\'; 49 typeof new Number(1) === \'object\'; 50 typeof new String(\'abc\') === \'object\'; 51 52 53 // Functions 54 typeof function() {} === \'function\'; 55 typeof class C {} === \'function\'; 56 typeof Math.sin === \'function\'; 57 58 59 // This stands since the beginning of javascript 60 typeof null === \'object\';
instanceof
object instanceof constructor (object: 要检测的对象 constructor: 某个构造函数 )
instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。
1 // 定义构造函数 2 function C(){} 3 function D(){} 4 5 var o = new C(); 6 7 // true,因为 Object.getPrototypeOf(o) === C.prototype 8 o instanceof C; 9 10 // false,因为 D.prototype不在o的原型链上 11 o instanceof D; 12 13 o instanceof Object; // true,因为Object.prototype.isPrototypeOf(o)返回true 14 C.prototype instanceof Object // true,同上 15 16 C.prototype = {}; 17 var o2 = new C(); 18 19 o2 instanceof C; // true 20 21 o instanceof C; // false,C.prototype指向了一个空对象,这个空对象不在o的原型链上. 22 23 D.prototype = new C(); // 继承 24 var o3 = new D(); 25 o3 instanceof D; // true 26 o3 instanceof C; // true
1 var simpleStr = "This is a simple string"; 2 var myString = new String(); 3 var newStr = new String("String created with constructor"); 4 var myDate = new Date(); 5 var myObj = {}; 6 7 simpleStr instanceof String; // returns false, 检查原型链会找到 undefined 8 myString instanceof String; // returns true 9 newStr instanceof String; // returns true 10 myString instanceof Object; // returns true 11 12 myObj instanceof Object; // returns true, despite an undefined prototype 13 ({}) instanceof Object; // returns true, 同上 14 15 myString instanceof Date; // returns false 16 17 myDate instanceof Date; // returns true 18 myDate instanceof Object; // returns true 19 myDate instanceof String; // returns false
以上是关于javascript中typeof和instanceof的主要内容,如果未能解决你的问题,请参考以下文章