关键词:
typeof 是一元操作符,而instanceof是二元操作符;
typeof 操作的是一个变量,而instanceof前面是一个变量,后面是一个类型;
typeof 返回的是一个字符串,而instanceof 返回的是一个布尔值。
1、typeof()
http://www.cnblogs.com/jikey/archive/2010/05/05/1728337.html typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。 示例 <!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> 结果 |
typeofhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof The 示例 // 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‘;
|
2、instanceof
instanceofhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof The The // 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()函数错误[重复]
】javascript中的typeof()函数错误[重复]【英文标题】:typeof()functionerrorsinjavascript[duplicate]【发布时间】:2012-08-0204:09:37【问题描述】:可能重复:Istherea(built-in)wayinJavaScripttocheckifastringisavalidnumber?我有一个HTML输入搜索栏<formclass... 查看详情
javascript学习笔记--数据类型
1.数据类型js的数据类型共有6种分别是:数值,字符串,布尔值,undefind,null,对象最基本的数据类型有3种:数值,字符串,和布尔值对象可以分为3个子类型:狭义的对象,数组,函数狭义的数组和对象是两种不同的数据组合方式。而... 查看详情
javascript中typeof和instanceof
typeof 查看详情
javascript里面的数据类型都有哪些
我们先来认识下JavaScript中的数据类型。JavaScript是弱类型语言,开始的时候并不知道变量时什么类型,必须通过存储的具体的值才能判断变量的类型。JavaScript的数据类型分为基本数据类型和复杂数据类型。其中,基本数据类型:Nu... 查看详情
javascript中typeof,instanceof,hasownproperty,in的用法和区别
一.typeof操作符typeof操作符用于返回正在使用值的类型。//使用原始值letmNull=null;letmUndefined=undefined;letmString=‘mazey‘;letmNumber=123;letmBoolean=true;letmFunction=function(){returntrue;};//用构造函数的方式new一个实例letoString=newStr 查看详情
javascript中的类型
1.变量是没有类型的,值(直接量)才有类型 。2.typeof的返回值有‘string‘,‘number‘,‘undefined‘,‘boolean‘,‘object‘。3.typeof对变量操作时,其实是返回变量的值的类型。4.一个一直存在,可能永远也不会的bug。typeofnull的返... 查看详情
typeof和instanceof的区别
...对象的类型。Class一列表示对象的内部属性[[Class]]的值。JavaScript标准文档中定义:[[Class]]的值只可能是下面字符串中的一个:Arguments,Array,Boolean 查看详情
前端最基础面试题:说说javascript中如何判断数据类型?(代码片段)
1.基本数据类型的判定:typeof[变量名]typeof1//'number'typeof'string呀'//'string'typeoftrue//'boolean'typeofSymbol('abc')//'symbol'控制台验证:扩充知识:typeofNaN//'number'。2.引用类型object的判断... 查看详情
java中用啥方法判断数据类型,就行javascript中的typeof一样
参考技术Apublic static void main(String[] args) throws Exception Integer i=0; String s=""; typeof(i.getClass()); typeof(s.getClass()); final static void typeof(Class<?> clzz) System.out.println("t... 查看详情
javascript使用构造函数获取变量的类型名
在JavaScript中,如何准确获取变量的类型名是一个经常使用的问题.但是常常不能获取到变量的精确名称,或者必须使用jQuery中的方法,这里我通过typeof,jQuery.type和通过构造函数来获取变量类型这三种方法详细介绍一遍.希望可以对你提... 查看详情
typeof和instanceof的认识
前言在javascript中,存在着六种基础数据类型,分为值类型(“undefined”,”boolean”,“Null”,“Number ”,“String”)和引用类型(“object ”,“Function”)instanceof在JavaScript中,判断一个变量的类型尝尝会用typeof运算... 查看详情
js中的typeof和instanceof和===
typeof: 用于判断number/string/boolean/underfined类型/function 不能判断:null和object,不能区分object和Arrayinstanceof: 判断具体的对象类型===: 用于判断undefined和null //五种基本类型varnum=1;varstr="abc";var 查看详情
javascript基础--typeof和数据类型转换(代码片段)
🌈前言变量的数据类型转换:将一种数据类型转换为另外一种数据类型。通常有三种形式的类型转换:转换为字符串类型转换为数字型转换为布尔型你会专门把某个数据类型转换成null或者undefined吗?不会,因... 查看详情
typeof,null,和undefined
typeof操作符可以用于检测变量的数据类型Null在JavaScript中null表示"什么都没有"。null是一个只有一个值的特殊类型。表示一个空对象引用。可以设置为null来清空对象:Undefined在JavaScript中, undefined 是一个没有设置值的变量。ty... 查看详情
javascript中的数字和数字有什么区别?[重复]
这个问题在这里已有答案: Whyis4notaninstanceofNumber?5个答案 输出:varx=5;typeof(x)//willgivenumbertypeasoutput但是对于下面的代码,它返回false:varx=5;xinstanceofNumber;//willgivefalseasoutput任何人都可以解释数字和数 查看详情
javascript类型
1.JavaScript typeof,null,和undefinedJavaScripttypeof, null,undefined,valueOf()。1.1typeof操作符typeof"John"//返回stringtypeof3.14//返回numbertypeoffalse//返回booleantypeof[1,2,3,4]//返回objecttypeof{name 查看详情
javascript基础学习
javascript的数据类型 学习要点: typeof操作符 五种简单数据类型:Undefined、String、Number、Null、Boolean 引用数据类型:数组和对象一、typeof操作符 typeof操作符用来检测变量的数据类型,操作符可以操作变... 查看详情
[typescript]type、typeof、keyof
...ipt中,typeof操作符用来获取一个变量或对象的类型;巧了JavaScript中也有typeof,先来复习下在JavaScript中的用法TypeScript中的typeof:typeof也可以获取函数的类型:keyof操作符可以用于获取某种类型的所有键,其返回类型是联合类型与typ... 查看详情