在jQuery中,为了屏蔽event对象在各浏览器中的差异性,它使用了自定的Event对象,如下:
1 jQuery.Event = function( src, props ) {
2 // Allow instantiation without the 'new' keyword 3 //instanceof 用于判断一个变量是否某个对象的实例 4 if ( !(this instanceof jQuery.Event) ) { 5 return new jQuery.Event( src, props ); 6 } 7 8 // Event object 9 if ( src && src.type ) { /* 如果是event对象 * */ this.originalEvent = src;//将原生的event对象存于Event中 this.type = src.type;//事件类型 // Events bubbling up the document may have been marked as prevented // by a handler lower down the tree; reflect the correct value. /* 修正isDefaultPrevented方法 * */ this.isDefaultPrevented = src.defaultPrevented || src.defaultPrevented === undefined && // Support: IE < 9, Android < 4.0 src.returnValue === false ? returnTrue : returnFalse; // Event type } else { //如果event是事件名称 this.type = src; } // Put explicitly provided properties onto the event object //赋值自定义属性 if ( props ) { jQuery.extend( this, props ); } // Create a timestamp if incoming event doesn't have one this.timeStamp = src && src.timeStamp || jQuery.now();//时间戳 // Mark it as fixed this[ jQuery.expando ] = true;//标识event已被处理过 };
这个对象不复杂,对吧!