Reading jquery's ten (some flaws)

Source: Internet
Author: User

JQuery1.6.1 has been released for a while and some redundant code has been found, as listed below:

 

1. bind method. The last fn parameter is redundant.

View sourceprint? 1 // Handle object literals

2 if (typeof type = "object "){

3 for (var key in type ){

4 this [name] (key, data, type [key], fn );

5}

6 return this;

7}

 

2. Comment

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

Change

View sourceprint? 1 // 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.

View sourceprint? 1 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.

View sourceprint? 1 elemData. handle = eventHandle = function (e ){

2 // Discard the second event of a jQuery. event. trigger () and

3 // when an event is called after a page has unloaded

4 return typeof jQuery! = "Undefined "&&(! E | jQuery. event. triggered! = E. type )?

5 jQuery. event. handle. apply (eventHandle. elem, arguments ):

6 undefined;

7

8 };

 

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

View sourceprint? 1 if (elem. addEventListener ){

2 elem. addEventListener (type, eventHandle, false );

3

4} else if (elem. attachEvent ){

5 elem. attachEvent ("on" + type, eventHandle );

6}

Replace the following

View sourceprint? 01 jQuery. addEvent = document. addEventListener?

02 function (elem, type, handle ){

03 if (elem. addEventListener ){

04 elem. addEventListener (type, handle, false );

05}

06 }:

07 function (elem, type, handle ){

08 if (elem. attactEvent ){

09 elem. attactEvent ("on" + type, handle );

10}

11 };

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

 

Continuous update...

 

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.