-
Notifications
You must be signed in to change notification settings - Fork 17
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
JS设计模式观察者模式(发布/订阅模式) #22
Labels
Comments
@Kelichao 感觉以下代码有部分问题: // 方法就是人员,报社列一张订阅表,对应的报社在分发的时候执行对应列表的方法即可
// 人员1订阅了报纸1,报纸2
people1.subscribe("publisher1").subscribe("publisher2");
// 人员2订阅了报纸3,报纸4
people2.subscribe("publisher3").asubscribed("publisher4");
// 报社1 分发报纸1,2(触发事件)
publisher1.deliver(data);
// 报社2 分发报纸3,4(触发事件)
publisher2.deliver(data);
还有 建议改成如下: // 方法就是人员,报社列一张订阅表,对应的报社在分发的时候执行对应列表的方法即可
// 人员1订阅了报纸1,报纸2
people1.subscribe("publisher1").subscribe("publisher2");
// 人员2订阅了报纸3,报纸4
people2.subscribe("publisher3").subscribed("publisher4");
// 报社1 分发报纸,触发人员1的订阅,于是人员1收到报社1的报纸
publisher1.deliver(data);
// 报社2 分发报纸,触发人员1的订阅,于是人员1收到报社2的报纸
publisher2.deliver(data);
// 报社3 分发报纸,触发人员2的订阅,于是人员2收到报社3的报纸
publisher3.deliver(data);//增加
// 报社4 分发报纸,触发人员2的订阅,于是人员2收到报社4的报纸
publisher4.deliver(data);//增加 若有错请指正~ 谢谢指正 |
通俗易懂,不过场景有哪些啊,vue和react的双向绑定是defineProperty和subscribe结合起来,实现的吗? |
这个部分是不是改成
才能正常运行? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
发布订阅模式
下面模拟一下场景
模拟一个简单的订阅模式
订阅发布模式的实践
源码地址
源码链接
生成函数列队
demo链接
Promise包装器
demo链接
The text was updated successfully, but these errors were encountered: