Reading jQuery's nine flaws _ jquery

Source: Internet
Author: User
JQuery1.6.1 has been released for some time. Some redundant code is found, listing 1. bind method. The last fn parameter is redundant.

The Code is as follows:


// Handle object literals
If (typeof type = "object "){
For (var key in type ){
This [name] (key, data, type [key], fn );
}
Return this;
}


2. Comment

The Code is as follows:


// Add which for click: 1 === left; 2 === middle; 3 === right


Change

The Code is as follows:


// Add which for mousedown/mouseup: 1 === left; 2 === middle; 3 === right


3. The data method defines the local variable internalKey, but the subsequent code still follows jQuery. expando.

The Code is as follows:


Var internalKey = jQuery. expando, getByName = typeof name = "string", thisCache,


4. jQuery. event. add method, change arguments to e, and change apply to call to be better. Because only one parameter is passed: the event object.

The Code is as follows:


ElemData. handle = eventHandle = function (e ){
// Discard the second event of a jQuery. event. trigger () and
// When an event is called after a page has unloaded
Return typeof jQuery! = "Undefined "&&(! E | jQuery. event. triggered! = E. type )?
JQuery. event. handle. apply (eventHandle. elem, arguments ):
Undefined;

};


5. Based on the "do not repeat the browser" principle, jQuery. event. add the following code:

The Code is as follows:


If (elem. addEventListener ){
Elem. addEventListener (type, eventHandle, false );

} Else if (elem. attachEvent ){
Elem. attachEvent ("on" + type, eventHandle );
}


Replace the following

The Code is as follows:


JQuery. addEvent = document. addEventListener?
Function (elem, type, handle ){
If (elem. addEventListener ){
Elem. addEventListener (type, handle, false );
}
}:
Function (elem, type, handle ){
If (elem. attactEvent ){
Elem. attactEvent ("on" + type, handle );
}
};


In fact, jQuery already has jQuery. removeEvent, but I don't know why jQuery. addEvent does not exist.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.