{"id":29926588,"url":"https://github.com/ecomfe/saber-emitter","last_synced_at":"2025-11-05T20:02:13.492Z","repository":{"id":9040822,"uuid":"10803637","full_name":"ecomfe/saber-emitter","owner":"ecomfe","description":"一个适用于移动端的事件发射器","archived":false,"fork":false,"pushed_at":"2015-06-18T05:42:40.000Z","size":448,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-07-22T05:16:59.886Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecomfe.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-19T18:53:22.000Z","updated_at":"2017-03-16T07:14:37.000Z","dependencies_parsed_at":"2022-09-11T04:40:12.195Z","dependency_job_id":null,"html_url":"https://github.com/ecomfe/saber-emitter","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/ecomfe/saber-emitter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-emitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-emitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-emitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-emitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecomfe","download_url":"https://codeload.github.com/ecomfe/saber-emitter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-emitter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268392165,"owners_count":24243297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-02T12:42:08.545Z","updated_at":"2025-11-05T20:02:13.440Z","avatar_url":"https://github.com/ecomfe.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"saber-emitter\n===\n\n![Bower version](https://img.shields.io/bower/v/saber-emitter.svg?style=flat-square) [![NPM version](https://img.shields.io/npm/v/saber-emitter.svg?style=flat-square)](https://npmjs.org/package/saber-emitter) [![Build Status](https://img.shields.io/travis/ecomfe/saber-emitter.svg?style=flat-square)](https://travis-ci.org/ecomfe/saber-emitter) [![License](https://img.shields.io/npm/l/saber-emitter.svg?style=flat-square)](./LICENSE) [![EFE Mobile Team](https://img.shields.io/badge/EFE-Mobile_Team-blue.svg?style=flat-square)](http://efe.baidu.com)\n\n一个适用于移动端的事件发射器，兼容 node 环境。\n\n## Installation\n\n通过 [edp](https://github.com/ecomfe/edp) 引入模块：\n\n```sh\n$ edp import saber-emitter\n```\n\n或者在 node 环境下 使用 `npm` 引入模块：\n\n```sh\n$ npm install saber-emitter --save\n```\n\n## Usage\n\n```js\nrequire(['saber-emitter'], function(Emitter) {\n    var emitter = new Emitter();\n\n    emitter.on('greeting', function(name) {\n        console.log('Hello, ' + name + '!');\n    });\n\n    emitter.emit('greeting', 'Firede');\n});\n```\n\n## API\n\n创建 `Emitter` 实例\n\n* _return_ `{Emitter}`\n\n```js\nvar Emitter = require('saber-emitter');\nvar emitter = new Emitter();\n```\n\n### Methods\n\n#### mixin(obj)\n\n通过 `mixin` 将 `Emitter` 混入目标对象\n\n* **obj** `{Object}` 目标对象\n* _return_ `{Object}` 混入 Emitter 后的目标对象\n\n```js\nvar obj = {};\nEmitter.mixin(obj);\n\nobj.emit('foo');\n```\n\n### Classes\n\n#### on(event, listener)\n\n挂载事件\n\n* **event** `{string}` 事件名\n* **listener** `{Function}` 监听器\n* _return_ `{Emitter}`\n\n```js\nfunction listenerFn(name) {\n    console.log('Hello ' + name);\n}\nemitter.on('say', listenerFn);\n```\n\n#### once(event, listener)\n\n挂载只执行一次的事件\n\n* **event** `{string}` 事件名\n* **listener** `{Function}` 监听器\n* _return_ `{Emitter}`\n\n```js\nemitter.once('say', listenerFn);\n```\n\n#### off([event[, listener]])\n\n注销事件与监听器\n\n* **event** `{string=}` 事件名\n* **listener** `{Function=}` 监听器\n* _return_ `{Emitter}`\n\n```js\n// `不传参数` 将注销当前实例的所有事件\nemitter.off();\n\n// 只传入 `event` 将注销该事件下挂载的所有监听器\nemitter.off('say');\n\n// 传入 `event` 与 `listener` 将只注销该监听器\nemitter.off('say', listenerFn);\n```\n\n#### emit(event[, ...args])\n\n触发事件\n\n* **event** `{string}` 事件名\n* **args** `{...*}` 传递给监听器的参数，可以有多个\n* _return_ `{Emitter}`\n\n```js\nemitter.emit('say');\nemitter.emit('say', 'hello');\nemitter.emit('say', 'hello', 'world');\n```\n\n#### listeners(event)\n\n返回指定事件的监听器列表\n\n* **event** `{string}` 事件名\n* _return_ `{Array}` 监听器列表\n\n```js\nvar listeners = emitter.listeners('say');\n```\n\n#### setMaxListeners(number)\n\n设置每个事件下，监听器的最大个数。为 `0` 时不限制，默认值是 `10`\n\n* **number** `{number}` 监听器个数\n* _return_ `{Emitter}`\n\n```js\nemitter.setMaxListeners(8);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fsaber-emitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecomfe%2Fsaber-emitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fsaber-emitter/lists"}