-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateEvent.js
More file actions
47 lines (46 loc) · 1.31 KB
/
createEvent.js
File metadata and controls
47 lines (46 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 创建自定义事件(CustomerEvent)
* ========================================================================
* @method createEvent
* @param {String} type - (必须)事件类型(名称)
* @param {Object} [detail] - (可选)传递给自定义事件的数据,默认为 null
* @param {Boolean} [bubbles] - (可选)是否支持冒泡,默认为 true
* @param {Boolean} [cancelable] - (可选)是否可以取消,默认为 true
* @returns {CustomEvent} - CustomerEvent 实例
*
* @example
* <div id="nav" class="nav">
* <a id="service" class="anchor" href="https://www.yaohaixiao.com/serivce">Service</a>
* <a id="help" class="anchor" href="https://www.yaohaixiao.com/help">Help</a>
* </div>
*
* const $nav = document.querySelector('#nav')
* const logEvent = createEvent('log', {
* name: 'Yao',
* hi() {
* console.log('hi!!!')
* }
* })
*
* const logHandler = function(evt) {
* console.log('detail', evt.detail)
* console.log('type', evt.type)
* }
*
* $nav.addEventListener('log', logHandler)
*
* $nav.dispatchEvent(logEvent)
*/
const createEvent = (
type,
detail = null,
bubbles = true,
cancelable = true
) => {
return new CustomEvent(type, {
detail: detail,
bubbles: bubbles,
cancelable: cancelable
})
}
export default createEvent