技术控

    今日:104| 主题:49431
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Do you even need that bind?

[复制链接]
分手后S朋友 发表于 2016-11-30 19:56:53
30 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Codereviewer: “Why is this binding necessary?”
  [code]        closeOnOuterClick={true}
       onClose={viewStore.closeModal.bind(viewStore)}[/code]   This code follows my Simple MobX-driven modals approach.
   Swiz: “Without binding, the callback would bind to Modal object when called, if I’m not mistaken.”
   Codereviewer: “Would it? That doesn’t seem very intuitive.”
   Swiz: “Welcome to JavaScript’s dynamic scoping. Where have you been the past few years?…
   But you’re right, it does seem to work. I think MobX’s @action does a bind for us. It definitely wouldn’t work with vanilla functions. I’ll change the code code.”
  
Do you even need that bind?-1 (necessary,function,dynamic,Welcome,correct)
   Nope, no, nah. I should not have done that. That did not work. @action does not do any binding. Wrapping a function in a function does not create a closure with the correct local scope.
   But it did work in that one case I tried. Just not in the other. ��
  Let’s investigate.

  Here’s a self-contained example in Codepen – a clicker.
   See the Pen React, mobx, bind example by Swizec Teller ( @swizec ) on CodePen .light
   We have a Store that holds the number of clicks and contains an inc action to increase the count.
     [code]class Store {
  @observable clicks = 0;

  @action inc() {
    this.clicks += 1;
  }
}[/code]     A not-very-smart functional stateless component renders the current count and a link to click. We use onClick with a bounded action to detect clicks.
     [code]const Clicky = observer(({ store }) => (
  

    Clicks: {store.clicks}
   

    +1
  

));[/code]

Do you even need that bind?-2 (necessary,function,dynamic,Welcome,correct)
  Click the link, number goes up. 14 lines of actual code.
   Ok, we know it works with a .bind . Will it work without one?
   See the Pen React, mobx, bind example, pt2 by Swizec Teller ( @swizec ) on CodePen .light
   We expanded our Clicky component with a link that uses an unbounded store.inc action call as the onClick callback.
     [code]const Clicky = observer(({ store }) => (
  

    Clicks: {store.clicks}
   

    +1 bound
   

    +1 unbound
  

));[/code]
12下一页
友荐云推荐




上一篇:This Week in Spring: Reactive Programming and Springing to the Cloud
下一篇:Quick TR069 Botnet Writeup + Triage
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

zhangyuedong 发表于 10 小时前
梦想不能实现,都是因为它不够现实。
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表