Pages
Categories
Archives
JsTweeterManiac
- first time was on conference using skype call(toll free number). Skype is really great! 1 month ago
- RT @creationix: Connect app on quad-core machine over a real network connection. 35231.05 Reqs/sec http://bit.ly/aCWnd5 #nodejs #connect 2 months ago
- some provelinks #V8 vs #php http://ow.ly/1Xbg4 vs #ruby http://ow.ly/1Xbhl vs #Perl http://ow.ly/1Xbii vs #python http://ow.ly/1Xbjk 2 months ago
- http://ow.ly/1X9Z8 about #nodejs: "It is much faster than Ruby, Python, or Perl." really??? 2 months ago
- dyuproject(openID+oAuth) http://bit.ly/cfLC1b + facebook-java-api http://bit.ly/CnjrW + native google login. 3 months ago
- Strange, but there are so many Java libs are not created yet. I need openID + oAuth(twitter) + Facebook connect. 3 months ago
- Who will come to chatroulet next to perverts? - Salesmans http://bit.ly/d6HWS1 Shit! 3 months ago
- backend development excites much more than frontend. Frontend is kind of a job. And job is boring:).... Just kidding. 3 months ago
- The Big Bang Theory: Everybody want to be Sheldon, Nobody wanna be Leonard:) 4 months ago
- Proud of myself. I have found bug in Spring framework. http://bit.ly/96Xglh 4 months 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