JS这些变量是弱类型(这是弱类型)的,它能够无论用来存储数据的类型的。javascript
typeof 数据类型可用于检测给定的变量。可能的返回值: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>")測试结果:
在 JavaScript 中,推断一个变量的类型常常会用 typeof 运算符。在使用 typeof 运算符时採用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”。html
这就需要用到instanceof来检測某个对象是否是还有一个对象的实例。java
一般来说。使用 instanceof 就是推断一个实例是否属于某种类型。instanceof 检測一个对象A是否是还有一个对象B的实例的原理是:查看对象B的prototype指向的对象是否在对象A的[[prototype]]链上。假设在,则返回true,假设不在则返回false。数组
只是有一个特殊的状况,当对象B的prototype为null将会报错(相似于空指针异常)。函数
測试:this
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參考资料
Object.prototype.toString().call(param) 返回param的类型(string,格式是[object class]) 。spa
toString() 方法可把一个逻辑值转换为字符串,并返回结果,语法为:booleanObject.toString()。刚才我说了,js中的对象都是继承的Object,这些对象都本身定义的有函数或者重构了Object的部分函数,而且它们都对toString()函数进行了重写。因此咱们不能像1中直接写param.prototype.toString()这样就运行的是param本身重写后的toString()函数了。
【
在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"。
】
.net
最后的最后提醒你们,Object.prototype.toString().call(param)返回的[object class]中class首字母是大写。像JSON这样的甚至都是大写,因此。你们推断的时候可以都转换成小写,以防出错,Object.prototype.toString().call(param).toLowerCase()就能够。prototype
測试:指针
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在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
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; },