当前位置 : 主页 > 网络编程 > JavaScript >

jquery框架之:判断js对象详细类型 jquery底层库的写法

来源:互联网 收集:自由互联 发布时间:2021-07-03
参考:http://www.cnblogs.com/flyjs/archive/2012/02/20/2360504.html 一般Array属于数组但是不能识别为Array而是object此方法可以识别详细的类型 1. [代码] test.html !doctype htmlhtml lang="en" head meta charset="UTF
参考:http://www.cnblogs.com/flyjs/archive/2012/02/20/2360504.html

一般Array属于数组 但是不能识别为Array 而是 object 此方法可以识别详细的类型

1. [代码]test.html    

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
 <a href="http://www.cnblogs.com/flyjs/archive/2012/02/20/2360504.html">  参考:http://www.cnblogs.com/flyjs/archive/2012/02/20/2360504.html</a>
  <script>
2016/5/12
	/**
 * Returns internal [[Class]] property of an object
 *
 * Ecma-262, 15.2.4.2
 * Object.prototype.toString( )
 *
 * When the toString method is called, the following steps are taken: 
 * 1. Get the [[Class]] property of this object. 
 * 2. Compute a string value by concatenating the three strings "[object ", Result (1), and "]". 
 * 3. Return Result (2).
 *
 * __getClass(5); // => "Number"
 * __getClass({}); // => "Object"
 * __getClass(/foo/); // => "RegExp"
 * __getClass(''); // => "String"
 * __getClass(true); // => "Boolean"
 * __getClass([]); // => "Array"
 * __getClass(undefined); // => "Window"
 * __getClass(Element); // => "Constructor"
 *
 */
function __getClass(object){
    return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
};
var ary = new Array();
var x = __getClass(ary);
alert(x);
  </script>
 </body>
</html>
网友评论