onfire.js 开源项目教程

onfire.js 开源项目教程

onfire.js:gun: onfire.js is a nano version (~500b) for event-emitter.项目地址:https://gitcode.com/gh_mirrors/on/onfire.js

1. 项目的目录结构及介绍

onfire.js 是一个简单的事件订阅和发布的 JavaScript 库。以下是其基本的目录结构:

onfire.js/
├── LICENSE
├── README.md
├── README_zh.md
├── package.json
├── src/
│   └── onfire.js
└── test/
    └── onfire.test.js

目录介绍:

  • LICENSE: 项目的许可证文件。
  • README.md: 项目的英文介绍文档。
  • README_zh.md: 项目的中文介绍文档。
  • package.json: 项目的 npm 配置文件,包含项目的基本信息和依赖。
  • src/: 源代码目录,包含主要的 JavaScript 文件。
  • test/: 测试代码目录,包含测试用例。

2. 项目的启动文件介绍

onfire.js 的启动文件位于 src/ 目录下,文件名为 onfire.js。这个文件是整个库的核心实现,包含了事件订阅、发布和取消订阅等功能。

启动文件内容概览:

(function (root, factory) {
  if (typeof module === 'object' && module.exports) {
    module.exports = factory();
  } else {
    root.onfire = factory();
  }
})(typeof window === 'undefined' ? this : window, function () {
  var __onfireEvents = [];
  var __cnt = 0; // event counter
  var string_str = 'string';
  var function_str = 'function';
  var hasOwnKey = Function.call.bind(Object.hasOwnProperty);
  var slice = Function.call.bind(Array.prototype.slice);

  function _bind(eventName, callback, is_one, context) {
    // 绑定事件的实现
  }

  function _fire_one(eventName, data) {
    // 触发单个事件的实现
  }

  function _un_one(eventName, callback) {
    // 取消单个事件绑定的实现
  }

  return {
    on: function (eventName, callback) {
      return _bind(eventName, callback, false, null);
    },
    one: function (eventName, callback) {
      return _bind(eventName, callback, true, null);
    },
    fire: function (eventName, data) {
      _fire_one(eventName, data);
    },
    un: function (eventObj) {
      _un_one(eventObj.eventName, eventObj.callback);
    },
    clear: function () {
      __onfireEvents = [];
      __cnt = 0;
    }
  };
});

主要功能:

  • on: 绑定事件。
  • one: 绑定一次性事件。
  • fire: 触发事件。
  • un: 取消事件绑定。
  • clear: 清空所有事件。

3. 项目的配置文件介绍

onfire.js 的配置文件是 package.json,这个文件包含了项目的基本信息、依赖和脚本命令等。

package.json 内容概览:

{
  "name": "onfire.js",
  "version": "2.0.0",
  "description": "A simple events dispatcher subscribe-publish library. Works on ie8+, firefox, chrome, safari, opera, android, ios",
  "main": "src/onfire.js",
  "scripts": {
    "test": "mocha test/onfire.test.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/hustcc/onfire.js.git"
  },
  "keywords": [
    "event",
    "subscribe",
    "publish",
    "dispatcher",
    "onfire"
  ],
  "author": "hustcc",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/hustcc/onfire.js/issues"
  },
  "homepage": "https://github.com/hustcc/onfire.js#readme",
  "devDependencies": {
    "mocha": "^5.

onfire.js:gun: onfire.js is a nano version (~500b) for event-emitter.项目地址:https://gitcode.com/gh_mirrors/on/onfire.js

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缪昱锨Hunter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值