自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

jQuery1.6c新增的適配器

開發(fā) 前端
由于JS用對(duì)象做為表進(jìn)行查找是比if條句與switch語(yǔ)句快很多,加之,適配器這種模式對(duì)于擴(kuò)展新功能非常有利,因?yàn)閖Query1.6便把它發(fā)揚(yáng)光大了。

其實(shí)在jQuery1.5中已有這東西,cssHooks,或者更早之前的jQuery.event.special, 或者 Sizzle.selectors里面更多的對(duì)象。它們共同的特點(diǎn)是包含了許多相關(guān)的函數(shù),cssHooks是專門處理css屬性的獲取與設(shè)置,如IE的opacity,event.special用于裝載與卸載submit, change, focus ,mouseenter等特別事件與自定義事件, Sizzle.selectors里面的過(guò)濾器與候選集獲取器就更不用說(shuō)了。由于JS用對(duì)象做為表進(jìn)行查找是比if條句與switch語(yǔ)句快很多,加之,適配器這種模式對(duì)于擴(kuò)展新功能非常有利,因?yàn)閖Query1.6便把它發(fā)揚(yáng)光大了。

在jQuery的attributes模塊(github是這樣分割的,但耦合這么高很難說(shuō)是模塊),共增加了三個(gè)這樣對(duì)象,valHooks,attrHooks, propHooks,分別對(duì)應(yīng)val,attr與prop這個(gè)三個(gè)方法。prop是新增的,表示jQuery決定區(qū)分屬性與特性的決心,但I(xiàn)E6/7還是無(wú)法區(qū)分它們,因此attr基本上涵蓋了prop的功能。

我們看一下它們各自的運(yùn)用吧!

  1. // jQuery.style 方法   
  2.       if ( value !== undefined ) {     
  3.         //=================略==============   
  4.         // If a hook was provided, use that value, otherwise just set the specified value     
  5.         if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) {    
  6.            // Wrapped to prevent IE from throwing errors when 'invalid' values are provided    
  7.            // Fixes bug #5509    
  8.           try {    
  9.              style[ name ] = value;    
  10.            } catch(e) {}    
  11.          }    
  12.     
  13.       } else {    
  14.          // If a hook was provided get the non-computed value from there    
  15.          if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) {    
  16.            return ret;    
  17.          }  
  18.         // Otherwise just get the value from the style object    
  19.         return style[ name ];    
  20.       }   
  1. // jQuery.fn.val 方法    
  2.       if ( !arguments.length ) {    
  3.         if ( elem ) {    
  4.           hooks = jQuery.valHooks[ elem.nodeName.toLowerCase() ] || jQuery.valHooks[ elem.type ];    
  5.           if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) {    
  6.             return ret;    
  7.          }    
  8.            return (elem.value || "").replace(rreturn, "");    
  9.         }    
  10.         return undefined;    
  11.      }    
  12.      //===============略============     
  13.       hooks = jQuery.valHooks[ this.nodeName.toLowerCase() ] || jQuery.valHooks[ this.type ];    
  14.       // If set returns undefined, fall back to normal setting    
  15.       if ( !hooks || ("set" in hooks && hooks.set( this, val, "value" ) === undefined) ) {    
  16.         this.value = val;    
  17.       }   
  18.  
  1. // jQuery.attr 方法    
  2.      hooks = jQuery.attrHooks[ name ] || ( jQuery.nodeName( elem, "form" ) && formHook );    
  3.       if ( value !== undefined ) {    
  4.         if ( value === null || (value === false && !rspecial.test( name )) ) {    
  5.           jQuery.removeAttr( elem, name );    
  6.           return undefined;    
  7.         } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {    
  8.           return ret;    
  9.         } else {    
  10.           // Set boolean attributes to the same name    
  11.          if ( value === true && !rspecial.test( name ) ) {    
  12.             value = name;    
  13.           }    
  14.          elem.setAttribute( name, "" + value );    
  15.           return value;    
  16.         }    
  17.       } else {    
  18.         if ( hooks && "get" in hooks && notxml ) {    
  19.           return hooks.get( elem, name );    
  20.         } else {    
  21.           ret = elem.getAttribute( name );    
  22.           // Non-existent attributes return null, we normalize to undefined    
  23.           return ret === null ? undefined : ret;    
  24.         }    
  25.       }   
  26.  
  1. // jQuery.prop 方法    
  2.            hooks = jQuery.propHooks[ name ];    
  3.         if ( value !== undefined ) {    
  4.             if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {    
  5.                 return ret;    
  6.             } else {    
  7.                 return (elem[ name ] = value);    
  8.            }    
  9.         } else {    
  10.             if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== undefined ) {    
  11.                 return ret;    
  12.             } else {    
  13.                 return elem[ name ];    
  14.             }    
  15.         }  
  1. //jQuery.event.add    
  2.       if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {    
  3.      // Bind the global event handler to the element    
  4.         if ( elem.addEventListener ) {    
  5.           elem.addEventListener( type, eventHandle, false );   
  6.         } else if ( elem.attachEvent ) {    
  7.           elem.attachEvent( "on" + type, eventHandle );    
  8.         }    
  9.       }    
  10. //jQuery.event.remove    
  11.       if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {    
  12.        jQuery.removeEvent( elem, type, elemData.handle );    
  13.    }   

可以發(fā)現(xiàn)還是很有章法的。這些適配器就是用來(lái)處理一些特殊的屬性,樣式或事件。而這些屬性,樣式或事件,我們可以通過(guò)瀏覽器的特征嗅探,把相應(yīng)的解決方法添加到適配器中。有了這些適配器,jQuery就可以省去許多if else 判定,當(dāng)正式版發(fā)布時(shí),又可以高興地宣布這幾個(gè)方法快了多少百分比了!

可以發(fā)現(xiàn)還是很有章法的。這些適配器就是用來(lái)處理一些特殊的屬性,樣式或事件。而這些屬性,樣式或事件,我們可以通過(guò)瀏覽器的特征嗅探,把相應(yīng)的解決方法添加到適配器中。有了這些適配器,jQuery就可以省去許多if else 判定,當(dāng)正式版發(fā)布時(shí),又可以高興地宣布這幾個(gè)方法快了多少百分比了!

可以發(fā)現(xiàn)還是很有章法的。這些適配器就是用來(lái)處理一些特殊的屬性,樣式或事件。而這些屬性,樣式或事件,我們可以通過(guò)瀏覽器的特征嗅探,把相應(yīng)的解決方法添加到適配器中。有了這些適配器,jQuery就可以省去許多if else 判定.
 

責(zé)任編輯:陳貽新 來(lái)源: Ruby's Louvre
相關(guān)推薦

2013-02-26 10:55:47

C#適配器設(shè)計(jì)模式

2012-09-19 15:29:26

Worklight適配器

2024-07-31 10:41:16

C#設(shè)計(jì)模式

2018-10-11 10:38:31

前端JavaScript編程語(yǔ)言

2015-08-07 10:05:37

recyclervie超省寫法

2022-02-18 17:21:29

適配器模式客戶端

2020-10-25 08:56:21

適配器模式

2021-02-16 08:16:09

適配器模式MybatisJava

2022-02-13 23:33:24

設(shè)計(jì)模式Java

2021-08-06 06:51:16

適配器配置Spring

2012-05-16 17:22:11

Java設(shè)計(jì)模式

2009-11-18 18:08:20

PHP適配器模式

2009-12-21 10:26:09

Oracle適配器

2013-11-26 16:39:21

Android設(shè)計(jì)模式

2021-02-18 08:39:28

設(shè)計(jì)模式場(chǎng)景

2010-07-09 12:53:30

HART協(xié)議

2012-08-02 10:46:34

JavaAdapter模式

2014-12-17 09:57:01

AndroidAdapteViewHolder

2014-07-17 10:55:10

Win8.1應(yīng)用開發(fā)適配器模式

2010-09-02 17:04:52

DHCP服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)