dojo之发布/订阅

dojo.subscribe(/*String*/ topic, /*Object|null*/ context, /*String|Function*/ method) 订阅一个主题

 dojo.publish(/*String*/ topic, /*Array*/ args) 发布一个主题

下面看一个示例:

var numberAccumulator = {
total:0,
add:function(x){this.total+= x;}
};

dojo.subscribe("Numbers",numberAccumulator,"add");

function showTotal(){
console.log("The total is " + numberAccumulator.total);
}

dojo.subscribe("Numbers",showTotal);

dojo.publish("Numbers",[1]);
dojo.publish("Numbers",[2]);

结果显示为:The total is 1        The total is 2

示例2

function topic(){
console.log("I'm the topic.");
}

topic._listeners = [];
topic.use = function(x){this._listeners[x]}


function listener(){
console.log("I'm the listener.");
}
function listener1(){
console.log("I'm the listener1.");
}

dojo.subscribe("Array",topic,"use");
dojo.publish("Array",[listener()]);
dojo.publish("Array",[listener1()]);

输出结果:(为函数topic添加属性和方法

I'm the listener.
I'm the listener1.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值