Pages
Categories
Archives
JsTweeterManiac
- #nodejs google plus fan page just crossed 1000 followers! http://t.co/IdbGqaXd 4 days ago
- #nodejs google plus fan page just crossed 800 followers http://t.co/IdbGqaXd ! 2 weeks ago
- hey @ryah {http://t.co/fdc8SCPR} you've done some cool stuff, I'm a fan via {http://t.co/2NPs9fNA} 2 weeks ago
- node.js makes programming fun again https://t.co/qgZwpOzL 3 weeks ago
- core #nodejs in #coffeescript? How about debugging this? http://t.co/iICMV30r 1 month ago
- 1999:Java - is for small projects,use C. 2005:Ruby - is for small projects,use Java. 2011:node.js - is for small projects, use Ruby for big 1 month ago
- >npm xmas 1 month ago
- WebStorm 3 officially out. With #nodejs and #coffeescriptsupport http://t.co/kCrF8Ba4 1 month ago
- Behold of #eval fanpage join #javascript #evil legions! https://t.co/9b2NotMs 1 month ago
- Position: fixed revisited http://t.co/jJ0fPsoy 1 month ago
-
RSS Links
-
Meta
take control over class constructor
Javascript doesn’t give us simple way to control object constructing. We have new operator, but we cannot control it. Unless we do some magic.
Lets assume we have an observer instance, few classes and we want every instance of this classes to be ‘observed’.
var observer=[]// simplest ever function foo(){ this.foo="foo"; .... } function bar(){ this.bar="bar"; .... } function observe(child){ //MAGIC childName=child.name||child.toString().match(/function\s*([^\(])\s*\(/)[1]; newConst=function(){ ret={}; child.apply(ret, arguments); observer.push(ret); return ret; }; newConst.name=childName; window[childName]=newConst; } observe(foo); observe(bar); var t1=new foo(); var t2=new bar(); observer // t1,t2