typeof + instanceof+toString+constructor什么推理javascript数据类型
一个、typeof
JS这些变量是弱类型(这是弱类型)的,它可以不管用来存储数据的类型的。
typeof 数据类型可用于检测给定的变量。可能的返回值:
1. 'undefined' --- 这个值没有定义;
2. 'boolean' --- 这个值是布尔值。
3. 'string' --- 这个值是字符串。
4. 'number' --- 这个值是数值;
5. 'object' --- 这个值是对象或null;
6. 'function' --- 这个值是函数。
測试:
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(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>")
測试结果:
typeof(1): number
typeof(NaN): number
typeof(Number.MIN_VALUE): number
typeof(Infinity): number
typeof("123"): string
typeof(true): boolean
typeof(window): object
typeof(document): object
typeof(null): object
typeof(eval): function
typeof(Date): function
typeof(sss): undefined
typeof(undefined): undefined
參考资料
http://javaeyetodj.iteye.com/blog/1199125
http://www.cnblogs.com/lidabo/archive/2011/12/29/2305770.html
二、instanceof
在 JavaScript 中,推断一个变量的类型经常会用 typeof 运算符。在使用 typeof 运算符时採用引用类型存储值会出现一个问题,不管引用的是什么类型的对象,它都返回 “object”。
这就须要用到instanceof来检測某个对象是不是还有一个对象的实例。
通常来讲。使用 instanceof 就是推断一个实例是否属于某种类型。
另外。更重的一点是 instanceof 能够在继承关系中用来推断一个实例是否属于它的父类型。上面的代码中是推断了一层继承关系中的父类,在多层继承关系中,instanceof 运算符相同适用。
instanceof 检測一个对象A是不是还有一个对象B的实例的原理是:查看对象B的prototype指向的对象是否在对象A的[[prototype]]链上。假设在,则返回true,假设不在则返回false。
只是有一个特殊的情况,当对象B的prototype为null将会报错(类似于空指针异常)。
測试:
function Foo(){}
Foo.prototype = new Aoo();//JavaScript 原型继承
var foo = new Foo();
console.log(foo instanceof Foo)//true
console.log(foo instanceof Aoo)//true
console.log(Object instanceof Object);//true
console.log(Function instanceof Function);//true
console.log(Number instanceof Number);//false
console.log(String instanceof String);//false
console.log(Function instanceof Object);//true
console.log(Foo instanceof Function);//true
console.log(Foo instanceof Foo);//false
參考资料
http://www.studyofnet.com/news/175.html
三、toString
Object.prototype.toString().call(param) 返回param的类型(string,格式是[object class]) 。
toString() 方法可把一个逻辑值转换为字符串,并返回结果,语法为:booleanObject.toString()。刚才我说了,js中的对象都是继承的Object,这些对象都自己定义的有函数或者重构了Object的部分函数,并且它们都对toString()函数进行了重写。所以我们不能像1中直接写param.prototype.toString()这样就运行的是param自己重写后的toString()函数了。
在toString方法被调用时,会运行以下的操作步骤:
1. 获取this对象的[[Class]]属性的值.
2. 计算出三个字符串"[object ", 第一步的操作结果Result(1), 以及 "]"连接后的新字符串.
3. 返回第二步的操作结果Result(2).
在ES3中,规范文档并没有总结出[[class]]内部属性一共同拥有几种,只是我们能够自己统计一下,原生对象的[[class]]内部属性的值一共同拥有10种.各自是:"Array", "Boolean", "Date", "Error", "Function", "Math", "Number", "Object","RegExp", "String".所以Object.prototype.toString()的输出结果就是这样的格式的字符串[object Array],[object Boolean]。
所以说toString能够推断:"Array", "Boolean", "Date", "Error", "Function", "Math", "Number", "Object","RegExp", "String"以上十种数据类型。
【
在ES5.1中,除了规范写的更具体一些以外,Object.prototype.toString方法和[[class]]内部属性的定义上也有一些变化,Object.prototype.toString方法的规范例如以下:
在toString方法被调用时,会运行以下的操作步骤:
1 假设this的值为undefined,则返回"[object Undefined]".
2 假设this的值为null,则返回"[object Null]".
3 让O成为调用ToObject(this)的结果.
4 让class成为O的内部属性[[Class]]的值.
5 返回三个字符串"[object ", class, 以及 "]"连接后的新字符串.
能够看出。比ES3多了1,2,3步.第1,2步属于新规则,比較特殊,由于"Undefined"和"Null"并不属于[[class]]属性的值。经统计,可返回的类型有"Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", "String"比ES3多了2种各自是arguments对象的[[class]]成了"Arguments"。而不是曾经的"Object",还有就是多个了全局对象JSON,它的[[class]]值为"JSON"。
】
最后的最后提醒大家,Object.prototype.toString().call(param)返回的[object class]中class首字母是大写。像JSON这样的甚至都是大写,所以。大家推断的时候能够都转换成小写,以防出错,Object.prototype.toString().call(param).toLowerCase()就可以。
測试:
document.write("{}.toString.call(1): " + {}.toString.call(1) + "<br>");
document.write("{}.toString.call(NaN): " + {}.toString.call(NaN) + "<br>");
document.write("{}.toString.call(Number.MIN_VALUE): " + {}.toString.call(Number.MIN_VALUE) + "<br>")
document.write("{}.toString.call(Infinity): " + {}.toString.call(Infinity) + "<br>")
document.write("{}.toString.call(\"123\"): " + {}.toString.call("123") + "<br>")
document.write("{}.toString.call(true): " + {}.toString.call(true) + "<br>")
document.write("{}.toString.call(window): " + {}.toString.call(window) + "<br>")
document.write("{}.toString.call(document): " + {}.toString.call(document) + "<br>")
document.write("{}.toString.call(null): " + {}.toString.call(null) + "<br>")
document.write("{}.toString.call(eval): " + {}.toString.call(eval) + "<br>")
document.write("{}.toString.call(Date): " + {}.toString.call(Date) + "<br>")
document.write("{}.toString.call(undefined): " + {}.toString.call(undefined) + "<br>")
document.write("{}.toString.call({}): " + {}.toString.call({}) + "<br>")
document.write("{}.toString.call(sss): " + {}.toString.call(sss) + "<br>")
測试结果:
{}.toString.call(1): [object Number]
{}.toString.call(NaN): [object Number]
{}.toString.call(Number.MIN_VALUE): [object Number]
{}.toString.call(Infinity): [object Number]
{}.toString.call("123"): [object String]
{}.toString.call(true): [object Boolean]
{}.toString.call(window): [object global]
{}.toString.call(document): [object HTMLDocument]
{}.toString.call(null): [object
Null]
{}.toString.call(eval): [object Function]
{}.toString.call(Date): [object Function]
{}.toString.call(undefined): [object
Undefined]
{}.toString.call({}): [object Object]
參考资料:
http://www.jb51.net/article/42864.htm
四、constructor
在W3C定义中的定义:constructor 属性返回对创建此对象的数组函数的引用。
就是返回对象相相应的构造函数。
从定义上来说跟instanceof不太一致,但效果都是一样的
注意: constructor 在类继承时会出错
比如:
function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var aObj = new A();
alert(aobj.constructor === B)// -----------> true;
alert(aobj.constructor === A) //-----------> false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
alert(aobj instanceof B) //----------------> true;
alert(aobj instanceof B) //----------------> true;
言归正传。解决construtor的问题一般是让对象的constructor手动指向自己:
aobj.constructor = A; //将自己的类赋值给对象的constructor属性
alert(aobj.constructor === A) //-----------> true;
alert(aobj.constructor === B) //-----------> false; //基类不会报true了;
測试:
console.log([].constructor == <strong>Array</strong>); // true
console.log({}.constructor == <strong>Object</strong>); // true
console.log("string".constructor == <strong>String</strong>); // true
console.log((123).constructor == <strong>Number</strong>); // true
console.log(true.constructor == <strong>Boolean</strong>); // true
console.log((new Date()).constructor == <strong>Date</strong>);//true
參考资料:http://blog.sina.com.cn/s/blog_51048da70101grz6.html
五、jQuery中的类型推断
type()方法
type: function( obj ) {
if ( obj == null ) {
return String( obj );
}
return typeof obj === "object" || typeof obj === "function" ?
class2type[ core_toString.call(obj) ] || "object" :
typeof obj;
},
版权声明:本文博客原创文章。博客,未经同意,不得转载。
typeof + instanceof+toString+constructor什么推理javascript数据类型的更多相关文章
- 深入剖析JavaScript中的数据类型判断(typeof instanceof prototype.constructor)
关于JavaScript中的类型判断,我想大部分JavaScripter 都很清楚 typeof 和 instanceof,却很少有人知道 constructor,以及constructor与前面二 ...
- JavaScript的三种类型检测typeof , instanceof , toString比较
1.typeof typeof是js的一个操作符,在类型检测中,几乎没有任何用处. typeof 返回一个表达式的数据类型的字符串,返回结果为javascript中的基本数据类型,包括:number. ...
- 判断数据类型(typeof&instanceof&toString)
一.数据类型 ES6规范中有7种数据类型,分别是基本类型和引用类型两大类 基本类型(简单类型.原始类型):String.Number.Boolean.Null.Undefined.Symbol 引用类 ...
- 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()
typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...
- 【JavaScript中typeof、toString、instanceof、constructor与in】
JavaScript中typeof.toString.instanceof.constructor与in JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行 ...
- JavaScript中typeof、toString、instanceof、constructor与in
JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...
- JavaScript 中typeof、instanceof 与 constructor 的区别?
typeof.instanceof 与 constructor 详解 typeof 一元运算符 返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,st ...
- 推断js中的类型:typeof / instanceof / constructor / prototype
怎样推断js中的类型呢,先举几个样例: var a = "jason"; var b = 123; var c = true; var d = [1,2,3]; var e = n ...
- javaScript 数据类型,变量的类型转换,typeof()可以判断变量类型
js的数据类型和常见隐式转化逻辑. 一.六种数据类型 原始类型(基本类型):按值访问,可以操作保存在变量中实际的值.原始类型汇总中null和undefined比较特殊. 引用类型:引用类型的值是保存在 ...
随机推荐
- ios-上拉电阻负载许多其他接口
想尝试拉加载意识到有多少开始了他的研究之旅,我看了两天做出最终的界面. 之所以这么慢是由于,我不知道要将上拉出现的view放在哪.就能在scrollView拉究竟部的时候被拉出来.还有就是怎么拉出来之 ...
- AWR报告生成
ORACLE数据库两个比較重要的问题查看报告:awrrpt.sql,ashrpt.sql 生成报告的脚本一般存放在例如以下路径: /home/TEST/db/tech_st/11.2.0/rdbms/ ...
- 移动web:图片切换(焦点图)
在web页面上图片切换(焦点图)效果实在是太常见了,PC端.移动端到处都有它的身影. 上次写了个tab选项卡的效果,在这里延续一下,改成图片切换的效果. 如果不需要自动播放,稍微修改下html标签.和 ...
- 抓取csdn上的各类别的文章 (制作csdn app 二)
转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23532797 这篇博客接着上一篇(Android 使用Fragment,View ...
- 矩形类定义【C++】
Description 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. Inp ...
- 国内外MD5在线解密网站
-http://www.cmd5.com/english.aspx (457,354,352,282) - http://www.md5crack.com - http://www.hashcheck ...
- java javaEE javaWEB J2EE程序猿猿程序是脑损伤,终身工作程序猿
这几天我越来越郁闷.程序员现在很火----特javaEE员. 但我觉得火只是给人们的工作程序员. 原因 javaweb该项目是非常大的.没听过那个码农能单独接到什么项目.仅仅能被人剥削. 有人不信,我 ...
- NYOJ 372 巧克力的
巧克力 时间限制:4000 ms | 内存限制:65535 KB 难度:2 描写叙述 布欧能够把人变成巧克力吃了来添加他的能量,也有可能降低. 如今布欧变了n*m个巧克力,并把巧克力排成一个n*m ...
- Linking pronunciation in English
1.constant+vowel stand up give up get up 2.vowel+vowel 2.1 i:/i/ei/ai/oi [j] stay up carry it 2.2 u: ...
- 前端project师的修真秘籍(css、javascript和其他)
以我的经验,大部分技术,熟读下列四类书籍就可以. 入门,用浅显的语言和方式讲述正确的道理和方法,如head first系列 全面,巨细无遗地探讨每一个细节,遇到疑难问题时往往能够在这里得到理论解答,如 ...