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
时间: 2024-08-02 10:53:32

Javascript中的typeof和instanceof的相关文章

关于JavaScript中的typeof与instanceof

JavaScript中typeof和instanceof可以用来判断一个数据的类型,什么时候选择使用typeof?什么时候选择使用instanceof? typeof运算符 typeof运算符返回值有以下几种 原始数据类型 typeof 123 // "number" typeof '123' // "string" typeof false // "boolean" 函数类型 function f() {} typeof f // "

ExtJs--14--Ext.typeOf() 与 javascript中的typeof很相似,只是在类型上进行了一点简单的扩展,其实可以直接看源代码就可以看得懂的

Ext.typeOf() // Ext.typeOf() 与 javascript中的typeof很相似,只是在类型上进行了一点简单的扩展,其实可以直接看源代码就可以看得懂的 // typeOf: function(value) { // var type, // typeToString; // // if (value === null) { // return 'null'; // } // // type = typeof value; // // if (type === 'undef

浅谈JS中的typeof和instanceof的区别

JS中的typeof和instanceof常用来判断一个变量是否为空,或者是什么类型. typeof typeof运算符返回一个用来表示表达式的数据类型的字符串. typeof一般返回以下几个字符串: "number", "string","boolean","object","function","undefined" 对于Array,Null等特殊对象使用typeof一律返回obje

javascript中原型链与instanceof 原理

instanceof:用来判断实例是否是属于某个对象,这个判断依据是什么呢? 首先,了解一下javascript中的原型继承的基础知识: javascript中的对象都有一个__proto__属性,这个是对象的隐式原型,指向该对象的原型对象.显式的原型对象使用prototype,但是Object.prototype.__proto__=null; 判断某个对象a是否属于某个类A的实例,可以通过搜索原型链. //继承机制 function A(){ } A.prototype.name='licu

JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty

转自:http://www.cnblogs.com/ArthurXml/p/6555509.html 1.hasOwnProperty和in 先来理解hasOwnProperty方法.这个方法是用来检查对象的非原型链属性,换句话说,也就是检查对象中用户自定义的属性,而且这些属性不是定义在prototype上的.通过下面的代码进行理解: var myFunc=function(){ this.foo='value'; }; myFunc.prototype.ok='ok'; thefunc=new

JavaScript中的typeof操作符用法实例

在Web前端开发中,我们经常需要判断变量的数据类型.鉴于ECMAScript是松散类型的,因此需要有一种手段来检测给定变量的数据类型——typeof就是负责提供这方便信息的操作符. 对一个值使用typeof操作符可能返回下列某个字符串: “undefined”——如果这个值未定义“boolean”——如果这个值是布尔值“string”——如果这个值是字符串“number”——如果这个值是数值“object”——如果这个是对象或null“function”——如果这个值是函数 常用的typeof操

在 JavaScript 中为什么 typeof null 的结果是 object?

java 中的 null:既是对象,又不是对象,史称「薛定谔的对象」. typeof null==='object';  ..//true null instanceof Object  //false nullinstanceofObject===false 而 null instanceof null 会抛出异常: UncaughtTypeError:Right-hand side of 'instanceof'isnotan object 这是一个历史遗留下来的 feature(or bu

js中的typeof和instanceof和===

typeof: 用于判断number/string/boolean/underfined类型/function 不能判断:null和object ,不能区分object和Array instanceof: 判断具体的对象类型 ===: 用于判断undefined和null //五种基本类型 var num=1; var str="abc"; var bl=true; var nu=null; var undef=undefined; //三种特殊类型 var obj=new Objec

js中对 typeof 以及 instanceof 的使用及理解

F&Q: Q1:instanceof 和 typeof 是什么? 用于判断数据类型. Q2:它们能做什么? 根据数据类型判断进行一些逻辑编码.如:当判断出该参数类型为 number 则转换成 String 放入数组. Q3:为什么说 typeof 一般用于基本类型  instanceof 一般用于引用类型? 因为便利. 引用类型如果用typeof则会显示object类型,而这样区分不了这个参数是对象还是数组还是其他的引用类型.如果用instanceof则可以轻松区分. 而基本类型用instanc