jQuery.type()

jQuery.type()

jQuery.type( obj )Returns: String

Description: Determine the internal JavaScript [[Class]] of an object.

A number of techniques are used to determine the exact return value for an object. The [[Class]] is determined as follows:

  • If the object is undefined or null, then "undefined" or "null" is returned accordingly.
    • jQuery.type( undefined ) === "undefined"
    • jQuery.type() === "undefined"
    • jQuery.type( window.notDefined ) === "undefined"
    • jQuery.type( null ) === "null"
  • If the argument is either a primitive value or an instance of a standard built-in ECMAScript object, the [[Class]] internal property is used to determine the type. (More details about this technique.)
    • jQuery.type( true ) === "boolean"
    • jQuery.type( new Boolean() ) === "boolean"
    • jQuery.type( 3 ) === "number"
    • jQuery.type( new Number(3) ) === "number"
    • jQuery.type( "test" ) === "string"
    • jQuery.type( new String("test") ) === "string"
    • jQuery.type( function(){} ) === "function"
    • jQuery.type( [] ) === "array"
    • jQuery.type( new Array() ) === "array"
    • jQuery.type( new Date() ) === "date"
    • jQuery.type( new Error() ) === "error" // as of jQuery 1.9
    • jQuery.type( Symbol() ) === "symbol" // as of jQuery 1.9
    • jQuery.type( Object(Symbol()) ) === "symbol" // as of jQuery 1.12
    • jQuery.type( /test/ ) === "regexp"
  • Everything else returns "object" as its type.

Example:

Find out if the parameter is a RegExp.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.type demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js" rel="external nofollow" ></script>
</head>
<body>
 
Is it a RegExp? <b></b>
 
<script>
$( "b" ).append( "" + jQuery.type( /test/ ) );
</script>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/jQuery.type

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部