Javascript中的typeof和instanceof

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript中的typeof和instanceof相关的知识,希望对你有一定的参考价值。


typeof 是一元操作符,而instanceof是二元操作符;

typeof 操作的是一个变量,而instanceof前面是一个变量,后面是一个类型;

typeof 返回的是一个字符串,而instanceof 返回的是一个布尔值。


1、typeof()


http://www.cnblogs.com/jikey/archive/2010/05/05/1728337.html


typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。
它返回值是一个字符串,该字符串说明运算数的类型。,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined。 


示例

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>javascript测试</title>
    <script type="text/javascript">
        document.write ("typeof(1): "+typeof(1)+"<br />");
        document.write ("typeof(NaN): "+typeof(NaN)+"<br />");
        document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br />");
        document.write ("typeof(Infinity): "+typeof(Infinity)+"<br />");
        document.write ("typeof(\"123\"): "+typeof("123")+"<br />");
        document.write ("typeof(true): "+typeof(true)+"<br />");
        document.write ("typeof(window): "+typeof(window)+"<br />");
        document.write ("typeof(Array()): "+typeof(new Array())+"<br />");
        document.write ("typeof(function(){}): "+typeof(function(){})+"<br />");
        document.write ("typeof(document): "+typeof(document)+"<br />");
        document.write ("typeof(null): "+typeof(null)+"<br />");
        document.write ("typeof(eval): "+typeof(eval)+"<br />");
        document.write ("typeof(Date): "+typeof(Date)+"<br />");
        document.write ("typeof(sss): "+typeof(sss)+"<br />");
        document.write ("typeof(undefined): "+typeof(undefined)+"<br />")
    </script>
</head>
<body>
    
</body>
</html>

结果

技术分享



typeof

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof


The typeof operator returns a string indicating the type of the unevaluated operand.

技术分享

技术分享

示例

// Numbers
typeof 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!


// Strings
typeof "" === ‘string‘;
typeof "bla" === ‘string‘;
typeof (typeof 1) === ‘string‘; // typeof always returns a string
typeof String("abc") === ‘string‘; // but never use this form!


// Booleans
typeof true === ‘boolean‘;
typeof false === ‘boolean‘;
typeof Boolean(true) === ‘boolean‘; // but never use this form!


// Symbols
typeof Symbol() === ‘symbol‘
typeof Symbol(‘foo‘) === ‘symbol‘
typeof Symbol.iterator === ‘symbol‘


// Undefined
typeof undefined === ‘undefined‘;
typeof declaredButUndefinedVariable === ‘undefined‘;
typeof undeclaredVariable === ‘undefined‘; 


// Objects
typeof {a:1} === ‘object‘;

// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [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‘;


// Functions
typeof function(){} === ‘function‘;
typeof class C {} === ‘function‘;
typeof Math.sin === ‘function‘;

null

// This stands since the beginning of JavaScript
typeof null === ‘object‘;


In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value. (reference)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted intypeof null === ‘null‘.





2、instanceof


instanceof

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof


The instanceof operator tests whether an object has  the prototype property of a constructor in its prototype chain.

技术分享

The instanceof operator tests presence of constructor.prototype in object‘s prototype chain.

// defining constructors
function C(){}
function D(){}

var o = new C();

alert(o instanceof C);// true, because: Object.getPrototypeOf(o) === C.prototype
alert(o instanceof D);// false, because D.prototype is nowhere in o‘s prototype chain
alert(o instanceof Object);// true
alert(C.prototype instanceof Object);// true

C.prototype = {};
var o2 = new C();
alert(o2 instanceof C);//true

alert(o instanceof C);// false, because C.prototype is nowhere in o‘s prototype chain anymore

D.prototype = new C();// use inheritance
var o3 = new D();
alert(o3 instanceof D);//true
alert(o3 instanceof C);//true






以上是关于Javascript中的typeof和instanceof的主要内容,如果未能解决你的问题,请参考以下文章

关于JavaScript中的typeof与instanceof

javascript中的typeof和类型判断

javascript中的typeof()函数错误[重复]

判断JavaScript值的类型可以用typeof和instanceof

java中用啥方法判断数据类型,就行 javascript中的 typeOf一样

JavaScript typeof()