Giter Club home page Giter Club logo

events's Introduction

events

1. events是什么

enevts是一个简单的pub/sub模型,可用于对象事件拓展,也可独立作为事件发布接收源。

2. 如何使用

可以单独使用events作为事件源,用于发布、订阅事件,也可将events扩展至你自己的类、模块上。

2.1 以事件源的形式使用

//创建事件源
var eventsCenter = new util.events();
//监听某个事件
eventsCenter.on('add',function(){
    //do something
});
//主动触发自定义事件
eventsCenter.emit('add');

2.2 拓展已有对象

扩展对象时要避免使用 _events、'on'、'un'、'emit' 这四个字段,避免引起不必要的冲突。

2.2.1 拷贝继承式

/**
 * 这是你自己的类 
 **/
function myClass(){
    var me = this;
    // 继承事件
    util.events.extend( this );
    
    setInterval(function(){
        // 主动触发刷新事件
        me.emit('refresh');
    }, 2000);
}
 
// 实例化自己的类
var newclass = new myClass();
// 监听刷新事件
newclass.on('refresh',function(){
    // do somthing
});

2.2.2 原型链式

/**
 * 这是你自己的类 
 **/
function myClass(){
    var me = this;

    setInterval(function(){
        //主动触发某个事件
        me.emit('refresh');
	},2000);
}
// 创建原型对象
myClass.prototype = new util.events();

// 实例化自己的类
var newclass = new myClass();
// 监听刷新事件
newclass.on('refresh',function(){
	// do somthing
});
 

events's People

Contributors

bh-lay avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

events's Issues

多了个点

factory.events 后面多了个点

    global.util.events = factory.events.;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.