typeof和instanceof

08-08 体育 投稿:须夜卉

typeof和instanceof是JavaScript中的两个操作符。一般情况下对于基本类型我们使用typeof,对于对象的具体类型用instanceof。

typeof

typeof操作符返回一个字符串,表示未经求值的操作数(unevaluated operand)的类型。下面的表格总结了 typeof 可能的返回值。

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
// Numberstypeof 37 === 'number';typeof 3.14 === 'number';typeof(42) === 'number';typeof Math.LN2 === 'number';typeof Infinity === 'number';typeof NaN === 'number'; // Despite being "Not-A-Number"typeof Number(1) === 'number'; // but never use this form!// Stringstypeof '' === 'string';typeof 'bla' === 'string';typeof '1' === 'string'; // note that a number within a string is still typeof stringtypeof (typeof 1) === 'string'; // typeof always returns a stringtypeof String('abc') === 'string'; // but never use this form!// Booleanstypeof true === 'boolean';typeof false === 'boolean';typeof Boolean(true) === 'boolean'; // but never use this form!// Symbolstypeof Symbol() === 'symbol'typeof Symbol('foo') === 'symbol'typeof Symbol.iterator === 'symbol'// Undefinedtypeof undefined === 'undefined';// 定义为初始化typeof declaredButUndefinedVariable === 'undefined';// 未定义typeof undeclaredVariable === 'undefined';// Objectstypeof a: 1 === 'object';// use Array.isArray or Object.prototype.toString.call// to differentiate regular objects from arraystypeof [1, 2, 4] === 'object';typeof new Date() === 'object';// The following is confusing. Don't use!typeof new Boolean(true) === 'object';typeof new Number(1) === 'object';typeof new String('abc') === 'object';// Functionstypeof function() === 'function';typeof class C === 'function';typeof Math.sin === 'function';

注意事项:

12345678910
// null is a null Objecttypeof null === 'object'; // truetypeof NaN === 'number'; // true// 其它都是objecttypeof new Date() === 'object'; // truetypeof new Boolean(true) === 'object'; // truetypeof new Number(1) ==== 'object'; // truetypeof new String("abc") === 'object'; // true

instanceof

instanceof运算符可以用来判断某个构造函数的prototype属性所指向的對象是否存在于另外一个要检测对象的原型链上。

instanceof运算符希望左操作数是一个对象,右操作数标识对象的类。如果左侧的对象是右侧类的实例,则表达式返回true;否则返回false。

为了计算表达式o instanceof f,JavaScript首先计算f.prototype,然后在原型链中查找o,如果找到,那么o是f(或者f的父类)的一个实例,表达式返回true。如果f.prototype不在o的原型链中的话,那么o就不是f的实例,instanceof返回false。

原型链

1234567891011121314151617
function Foo(y)  this.y = y; Foo.prototype.x = 10;Foo.prototype.calculate = function()  // ...  var b = new Foo(20);var c = new Foo(30);b.__proto__ === Foo.prototype; // true b.__proto__ === Object.getPrototypeOf(b); // true b instanceof Foo; // true Foo.prototype.isPrototypeOf(b); // true

JavaScript中有个原型链的概念,这个链一般是通过对象的__proto__属性来建立的。prototype代表原型,prototype对象都有一个constructor属性,指向它的构造函数。这里的instanceof就是去判断是否在一条原型链上。

Object.prototype.toString()

每个对象都有一个toString()方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString()方法被每个Object对象继承。如果此方法在自定义对象中未被覆盖,toString() 返回 [object type],其中type是对象的类型。以下代码说明了这一点:

可以通过toString() 来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString() (避免出现 null.toString() 和 undefined.toString() 的情况)来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为thisArg。

Object.prototype.toString.call(v);

声明:生活头条网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系admin@gdcyjd.com