Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Reusable Abstractions in CoffeeScript (github.com/raganwald)
39 points by raganwald on Jan 30, 2012 | hide | past | favorite | 4 comments


As I mentioned in a tweet to raganwald the problem with this approach is that the object becomes opaque and can no longer be manipulated. ClojureScript allows you to preserve the dual use.


Since JavaScript functions can have properties, you can construct such a function in pure CoffeeScript:

  ofunc =(obj = {})->
    fn =(k)->@[k]
    bound =(args...)=>
      fn.apply(bound, args)
    bound extends obj
  
  o = ofunc [1,2,3]
  > o(0)
  1
  > o[0] = 10
  10
Not that I recommend this, for the obvious reasons.


interesting, although I think this code creates a copy of the original object’s properties rather than proxying them. Mind you, that could be what you want some times...


Great article. Just want to point out Katy (https://github.com/raganwald/Katy) is a fantastic addition to CoffeeScript.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: