首页 > 代码库 > Jquery Types 小结

Jquery Types 小结

      JavaScript provides several built-in(内置的) datatypes. In addition to those, this page documents virtual types(虚类) like Selectors, enhanced pseudo-types(伪类) like Events and all and everything you wanted to know about Functions.

      jQuery除了包含原生JS中的内置数据类型(built-in datatype),还包括一些扩展的数据类型(virtual types),如Selectors、Events等。

一、Anything

在jQuery文档中任何虚拟类型(virtual type)的使用表明可以使用任何类型或应该被期望。

二、String

A string in JavaScript is an immutable(不变的) object that contains none, one or many characters.

(1)var typeOfStr = typeof "hello world";//typeOfStr为“string"

(2)内置方法(Built-in Methods):

"hello".charAt( 0 ) // "h"
"hello".toUpperCase() // "HELLO"
"Hello".toLowerCase() // "hello"
"hello".replace( /e|o/g, "x" ) // "hxllx"
"1,2,3".split( "," ) // [ "1", "2", "3" ]

(3)length属性:返回字符长度,比如"hello".length返回5

(4)String转换为Boolean:一个空字符串("")默认为false,而一个非空字符串为true(比如"hello")。

三、htmlString

在jQuery文档用于表示一个或多个DOM元素的String被指定为htmlString,通常创建和插入文档中。当作为jQuery()函数的一个参数传递时,该字符串如果是以<tag>开始和解析,直到最后>字符。示例如下:

$( "<b>hello</b>" ).appendTo( "body" );  // <body><b>hello</b></body>
$( "<b>hello</b>bye" ).appendTo( "body" );  // <body><b>hello</b></body>
$( "bye<b>hello</b>" ).appendTo( "body" );  // 语法错误(Syntax error), unrecognized expression: bye<b>hello</b>
$( $.parseHTML( "bye<b>hello</b>" ) ).appendTo( "body" );  // <body>bye<b>hello</b></body>
$( "<b>hello</b>wait<b>bye</b>" ).appendTo( "body" );  // <b>hello</b>wait<b>bye</b>:

四、Number

在原生JavaScript中,Number是64位格式IEEE 754双精度(double-precision)值。就像字符串是不可变的。所有常见操作符等同于在于c语言中可以应用于数字(+, -, *, /, % , =,  +=, -=, * =, /=, ++, --)。

(1)String转换为Boolean:If a number is zero, it defaults to false.

(2)Math,数学对象:

Math.PI // 3.141592653589793

Math.cos(Math.PI) // -1

(3)Parsing Numbers,转换为数字:parseInt和parseFloat方法

parseInt( "123" ) = 123 // (implicit decimal(十进制))
parseInt( "010" ) = 8 // (implicit octal(八进制))
parseInt( "0xCAFE" ) = 51966 // (implicit hexadecimal(十六进制))
parseInt( "010", 10 ) = 10 // (explicit decimal(十进制))
parseInt( "11", 2 ) = 3 // (explicit binary(二进制))
parseFloat( "10.10" ) = 10.1

(4)Numbers to Strings,数字转换为字符串

[1]当将Number粘到(append)字符串后的时候,将得到字符串。

       "" + 1 + 2; // "12"      "" + (1 + 2); // "3"   "" + 0.0000001; // "1e-7"   parseInt( 0.0000001 ); // 1 (!)

      [2]或者用强制类型转换:

      String(1) + String(2); //"12"   String(1 + 2); //"3"

(5)NaN and Infinity(Both NaN and Infinity are of type "number"):

如果对一个非数字字符串调用parseInt方法,将返回NaN(Not a Number),isNaN常用来检测一个变量是否数字类型,如下:

parseInt( "hello", 10 ) // NaN    isNaN( parseInt("hello", 10) ) // true

Infinity表示数值无穷大或无穷小,比如  1 / 0 // Infinity。

另外 NaN==NaN 返回false,但是 Infinity==Infinity 返回true。

(6)Integer 和 Float:分为表示整型和浮点型,都是数字类型。

五、Boolean:布尔类型,true或者false。

六、Object对象

JavaScript中的一切皆对象。对一个对象进行typeof运算返回 "object"。

var x = {};    var y = { name: "Pete", age: 15 };

对于上面的y对象,可以采用圆点获取属性值,比如y.name返回"Pete",y.age返回15

 

Jquery Types 小结