1.typeof 2.instanceof 3.constructor 4.$.type()/jquery.type() 5.prototype
1.typeof
typeof是一个运算符,有2种使用方式:typeof(表达式)和typeof 变量名,第一种是对表达式做运算,第二种是对变量做运算
typeof运算符的返回类型为字符串,值包括如下几种
1. 'undefined' --未定义的变量或值
2. 'boolean' --布尔类型的变量或值
3. 'string' --字符串类型的变量或值
4. 'number' --数字类型的变量或值
5. 'object' --对象类型的变量或值,或者null
2.instanceof
instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
用法:
result = object instanceof class
参数:
Result:布尔类型。
Object:必选项。任意对象表达式。
Class:必选项。任意已定义的对象类。
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
3.constructor
constructor 属性返回对创建此对象的数组函数的引用。
语法:object.constructor
举例: <script type="text/javascript"> var test=new Array(); if (test.constructor==Array
) { document.write("This is an Array"); } if (test.constructor==Boolean
) { document.write("This is a Boolean"); } if (test.constructor==Date
) { document.write("This is a Date"); } </script>
4.$.type()/jquery.type()
用来确定不同类型对象的数据类型。逻辑可以按以下方法确定
如果对象是undefined或null,则返回相应的“undefined”或“null”
jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined"
jQuery.type( null ) === "null"
4.prototype
prototype 属性使您有能力向对象添加属性和方法。
语法:object.prototype.name=value