String: 空字符串等于Boolean的false;
htmlString: 包括起始tag和结束tag的字符串,代表html代码;
Number:双精度64位,跟string一样属于immutable,基于c语言的运算符都适用于此值;0默认为false,!0为true;
Math函数,parseInt函数(各进制到int的转换);
转换为字符串,""+1+2==“12”,""+(1+3)==“4”......
NaN,非Number的对象会返回NaN,用isNaN函数判断;Number类型;
Infinity,1/0==infinity;Number类型;
Note:NaN==NaN is false,Infinity==Infinity is true;
Integer属于Number类型,必须为非浮点数;
Float属于Number类型,为浮点数;
Boolean: true or false;
Object: var x = {}; {}的类型为object,默认值false;
var y = {name: "Pete",age: 15};
使用dot访问对象的属性:y.name or y.age;
使用array索引访问,y[name],y[age]
迭代for(key in y){ //do sth with key, and access value using y[key]};
Note:使用其他library的时候,可能扩展了Object.prototype,so take care!如:jQuery.each(obj,function(key,value))){}
Note:javascript对象没有继承机制,a workaround是使用prototype满足继承关系:
var Employee = function(name, title) { this.name = name; this.title = title; this.greet = function() { if (this.canTalk) { console.log("Hi, I'm "+this.name+", the "+this.title); } } } var Customer = function(name) {this.name = name;} var Mime = function(name) {this.name = name;this.canTalk = false;} var Person = {
canTalk : true,
greet : function() { if (this.canTalk) {console.log("Hi, I'm "+this.name)}
} }
继承机制为prototype; Customer.prototype = Person; Employee.prototype = Person; Mime.prototype = Person; var bob = new Employee('Bob','Builder'); var joe = new Customer('Joe'); var rg = new Employee('Red Green','Handyman'); var mike = new Customer('Mike'); var mime = new Mime('Mime'); bob.greet(); joe.greet(); rg.greet(); mike.greet(); mime.greet(); Array: var x=[]; var y=[1,2,3];数组的类型为object,使用索引读写数组;无论空否,数组默认为true;
数组迭代使用y=a.length作为终止条件,只读一次属性,速度比使用a.length作为终止条件快;
for ( var i = 0, item; item = a[i]; i++ ) {// Do something with item}此时数组不能为空;
var x=[1,2,3]; jQuery.each(x,function(index,value){//do sth with index and value});
var x=[]; x.push(1); x[x.length]=2; x is [1,2];
support method: reverse, join, shift, etc.
Array<Type>在Jquery中表示方法需要的参数类型是数组,且元素类型是指定值;
PlainObject: jQuery.isPlainObject(object)判断对象是否由{}或new Object(){}创建;
Function: 命名函数和匿名函数;
Arguments
function log( x ) {console.log( typeof x, arguments.length );} log(); // "undefined", 0 log( 1 ); // "number", 1 log( "1", "2", "3" ); // "string", 3
Note:上面的log函数体中,arguments变量总是有效的,arguments变量有length,callee(调用者)等属性;
var awesome = function(){return arguments.callee;}
awesome()==awesome //true;
Context,Call,Apply
"this"引用当前context,默认引用window对象,在function中可以改变context;
Function.prototype.call() and Function.prototype.apply(); refer to here and here.
NOTE: call和apply的异同:1.无参数时两者无区别;2.函数带参数时候,call(this,argumentsList),而apply (this,argumentArray);
Scope: 变量的作用域;
Closures:
function create() { var counter = 0; return {
increment: function() {counter++;},
print:function() {console.log( counter );}
} } var c = create(); c.increment(); c.print(); The pattern allows you to create objects with methods that operate on data that isn't visible to the outside—the very basis of object-oriented programming.
Proxy Pattern
Callback:
Callback is a plain JavaScript function, or sometimes just an event;
$( "body" ).click(function( event ) { console.log( "clicked: " + event.target );
}); Some callbacks are required to return something, others make that return value optional. To prevent a form submission, a submit event handler can return false:
$( "#myform" ).submit(function() { return false; }); selector: some plugin will leverage jQuery's selector. |
for instance, validation plugin accepts a selector to specify a dependency, whether an input is required or not:
emailrules:{required:"#email:filled"}
Events: blur,focus, load, resize, scroll, unload, beforeunload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress,and keyup.
jQuery: jquery object is a collection of DOM elements, it behaves much like an array but not actually a Javascript Array;
you will use the jQuery() function to create a jQuery object. jQuery() can also be accessed by its familiar single-character alias of $(), unless you have called jQuery.noConflict() to disable this option. Many jQuery methods return the jQuery object itself, so that method calls can be chained:
$( "p" ).css( "color", "red" ).find( ".special" ).css( "color", "green" ); 会对jQuery对象进行筛选,如find(),这些操作会返回一个经过筛选的新的jQuery对象;需要返回前一个jQuery对象则用.end(). 出的jQuery对象为empty,则最后的操作对DOM没有影响。 XMLHttpRequest: 有些AJAX返回XHR object,或者作为参数传递给success/error/complete hanlders,可以在其他操作中使用这个对象。 NOTE:Ajax只有在request使用此对象的情况下才返回该对象。 |
jqXHR: 从jquery1.5开始,$.ajax()返回jqXHR对象,which is a superset of the XMLHTTPRequest object.
Deferred Object(延迟对象): 使用自我管理的callback queues注册多个callback,按需调用。
Promise Object:此对象提供延迟对象的方法子集(then,done,fail,always,pipe,isResolved,isRejected),防止用户更改延迟对象。
Callbacks Object:使用方法$.Callbacks()来create或者返回Callbacks对象,此对象用途广泛,比较强大的功能是用来管理callback lists。
XML Object:一个由浏览器的XML DOM parser创建的文档对象,一般来自一个代表XML的字符串,XML文档与HTML语法不同,但jQuery的大多数遍历或操作都适用于此对象(XML文档)。
仅作个人学习只用,转载注明原文出处,引自:http://api.jquery.com/Types/