Posts

Showing posts from February, 2008

[COW] The fastest JavaScript rand ... isn't it ?

Hi guys, the WebReflection COW is a function that's so simple as usual: the php like rand function, to create random number, or random boolean evaluation: function rand(min, max){ return max ? min + rand(max - min) : Math.random() * ++min << .5; }; // pseudo packed version, 62 bytes function rand(m,M){return M?m+rand(M-m):Math.random()*++m<<.5} What's new? Nothing, but could be useful :lol: ... and it has really good performances // boolean evaluation var testMe = rand(1) ? true : false; Above example is for boolean assignment for random things The function works as PHP one, creation of something casual apart. I mean, if you use rand(), why don't you use Math.random() ? So, basically, this function is to have a random number from 0 to N, where N is the min argument, or from min to max, where MAX is the second one. rand(2); // 0, 1 or 2 rand(2, 5); // 2, 3, 4 or 5 Have a nice WE

[ES4] ... too horrible to be an editor ...

... but probably simple enough for these milestones. Its name is ES4me, it's for windows and works in this way: download the file in your es4 folder, where there is the run.exe and the es4.bat file double click Basically, there is a place to write something, where for some reason TABS are usable only with CTRL + TAB instead of single TAB key, a panel where errors or output will be showed, and finally, 5 buttons: Run, to execute the code Clear, to clear the code in the area Add Before, to add area code before execution Get Before, to add in area the code added before Clear Before, to clear content added before Here some example: var i:int = 123; print(i); Press Run, and You'll read 123 on output panel. Now press clear, and area will be without text. Write again ... var i:int = 123; Without the print, now press Add Before, area will be clean again ... BUT, write this: print(i); and press Run, you'll read 123 in the output panel. This mean that precedent code is in ram, and

How to inject protected methods in JavaScript

As you know, JavaScript requires different strategies to emulate private methods. I personally wrote some weird experiment to automatically emulate private scope behaviour. However, my experiment was RegExp based ... a really bad way to emulate private scope behaviour for a lot of reasons. At the same time, you can implement private scope methods only inside the constructor and this means that you cannot base your code with both prototype and private scope. // non sense example function MyConstructor(value){ function privateMethod(){ return this.value; } this.get = function(){ return privateMethod.call(this); } this.value = value; }; MyConstructor.prototype.getAgain = function(){ return privateMethod.call(this); // error, privateMethod has a different scope }; Basically, the reason that make prototype better than constructor with privileged methods, as get one is, is that core doesn't need to parse and/or create functions for each instance. Another reason to prefer them

packed.it goes off line !

... or, to be honest, that's what it's gonna do in few days, but PHP version is out ! You can find every information about server side version of packed it directly in this page . I am not joking when I said that packed.it is probably the fastest optimized client files server you can find over the net, even with an uncompiled program language like PHP. The key is the JOT compiler, compile one time, serve all the time without paranoia, saving bandwidth for both server and client, avoiding the usage of gz handlers, avoiding runtime compression, avoiding whathever you want, and finally, having the possibility to serve js, css, or both with a single file. Discover by yourself how long server need to serve, for example, jQuery ... less than one millisecond without louds of requests ... please look at X-Served-In header, if you do not trust me :P What's new? You do not need to pass in packed.it site to compile your projects, you can just serve and compile them directly from yo

[PHP] PartialFunction and PartialMethod

I found really interesting this John Resig post , about partial functions in JavaScript. For a weird problem, I choosed to use the same behaviour, but with a different language: PHP Sometime PHP is more flexible than we think (at least me), and this is the result of my little experiment: class ReflectionPartialFunction extends ReflectionFunction { protected $args; public function __construct($name){ parent::__construct($name); $this->args = func_get_args(); array_splice($this->args, 0, 1); } public function invoke($args){ $args = func_get_args(); return $this->invokeArgs($args); } public function invokeArgs(array $args){ return parent::invokeArgs(array_merge($args, $this->args)); } public function rinvoke($args){ $args = func_get_args(); return $this->rinvokeArgs($args); } public function rinvokeArgs(array $args){ return parent::invokeArgs(array_merge($this->args, $args)); } } class ReflectionPartialMethod extends ReflectionMethod { prote

jQuery + jQuery UI + Full Flora Theme ... less than 32Kb

Don't You trust me? It's so diabolically simple with packed.it ... and more than a professional Web Developer complained about " why on heart noone talk about that service " :D It's not packer, it's not YUICompressor, It's the only one that cache both CSS and JavaScript in a single file ... it's for PHP4, PHP5, Python wsgi, Python psp, C#.NET ... so, what are you waiting for? Ok, I'll let you try this full package: jQuery 1.2.3 and jQuery UI 1.5b with full Flora Theme included . Enjoy ;) P.S. jQueryUI.html example file calls jQueryUI.php, jut change estension in your server if you want python or .NET

The fastest way to know if someone "did it" ...

I know I choosed a weird title, isn't it :? Today I have two small and useful tips and tricks for JavaScript. The first one, lets you know if someone extended the Object.prototype if((function(k){for(k in {})return true;return false})()){ // do stuff that care about Object prototype } else { // do stuff that doesn't care about Object prototypes } Simple, quick, and dirty ;) and this is another example: function didIt(k){for(k in {})return true;return false}; if(didIt()){ // stuff prototype } One thing to take care about is the usage of a function instead of a variable. Since some script could run other scripts dinamically, you can't trust in a single time check. The second trick is really useful for function that accept a switcher argument . For switcher argument I mean methods or function that accept true or false as argument. The simplest way to have a switcher is obviously sending the value, but in this case we can't have a default behaviour. Simple is to choose a

JSmile for jQuery ;)

As I said one post ago, I've created a jQuery dedicated version of my simple JSmile project :geek: Here you can find the demo page, qhle here the automatic generated source What's new? It's so simple $(function(){ $(document.body).smile(); }); Use $("one or more smiles containers").smile(true or false) to add/remove smiles from your pages. See you ;)

jQuery minor improvements

Update This post has an "official" link, now, directly in devpro :) -------------- These are just few fixes / improvements of some jQuery stuff. (function(o){for(var k in o)jQuery[k] = o[k]})({ // IMPROVED: Evalulates a script in a global context using a specified JS version, if any globalEval: function( data, version ) { /** Examples $.globalEval("let(a = 1)alert(a);"); // error $.globalEval("let(a = 1)alert(a);", 1.7); // OK, alert 1 (function(){ var test = 1; $.globalEval("test = 2"); alert(test); // 1 })(); alert(test); // 2 */ data = jQuery.trim( data ); if ( data ) { var script = document.createElement("script"); script.type = "text/javascript"; if ( version ) script.type += ";version=" + version; if ( jQuery.browser.msie ) script.text = data; else script.appendChild( document.createTextNode( data ) ); with(document.getElementsByTagName("head&q