Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day-24-自定义eventbus #24

Open
H246802 opened this issue Jan 1, 2019 · 1 comment
Open

day-24-自定义eventbus #24

H246802 opened this issue Jan 1, 2019 · 1 comment

Comments

@H246802
Copy link
Owner

H246802 commented Jan 1, 2019

写一个对象 Bus,提供 onemit 方法,on 实现监听,emit 实现触发,使用方式如下:

Bus.on('event', function(data) {
  console.log(data)
})

Bus.on('hi', function(data) {
  console.log(data)
})

Bus.emit('event', {name: 'hello'})
Bus.emit('hi', {to: 'hunger'})
Bus.emit('hi', {to: 'valley'})
@H246802
Copy link
Owner Author

H246802 commented Jan 2, 2019

let Bus = {
  // 使用event存储 Bus.on 的函数
  event:{},
  on:function(name,fn){
    this.event[name] = fn
  },
  emit:function(name,param){
    this.event[name](param)
  }
}

Bus.on('event', function(data) {
  console.log(data)
})

Bus.on('hi', function(data) {
  console.log(data)
})

Bus.emit('event', {name: 'hello'})
Bus.emit('hi', {to: 'hunger'})
Bus.emit('hi', {to: 'valley'})

@H246802 H246802 changed the title day-24-自定义-eventbus day-24-自定义eventbus Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant