Posts

RitaliaCamp

brainstorming and questions, questions, questions ... really interesting event but now I'm a bit confused :D Stay tuned on official wiki !

JavaScript Metaprogramming and MetaDOM

I found really interesting Adam McCrea presentation about JavaScript Metaprogramming but I'm not, absolutely, a Prototype fun :P I thought about another way to show Metaprogramming power and that's why I wrote a simple MetaDOM object . What can You do with MetaDOM? Nothing that You can't do with every JavaScript library, get elements, modify them ... but with this object You should use a sintax like this one: with(MetaDOM){ While("p").Do(AddPNumber); For("li").In("ul").Do(AddLiNumber); If("div").class("some-div").Then(WriteSomeDiv); If("*").class("paragraph"). Then(WriteSomeDiv). ElseIf("*").id("paragraph"). Then(WriteSomeParagraphId) ; With("body").Do(function(body){ body.innerHTML = ' MetaDOM Experiment ' + body.innerHTML; }); If("*").name("h2").Then(AddH3); }; Semmes cool? This is the example page , tested only with FireFox...

JavaScript constructor (did you know?)

Update! [2007/03/27] to know if a generic object is cracked we don't need to create a new instance of its constructor. The fastest, safest and correct way is just to know if this object is an instance of its constructor. return o instanceof o.constructor ? o.constructor : undefined; Just fixed the Constructor function ;) I'm absolutely a JavaScript noob! Just today I "discovered" that a generic object constructor is not a read-only parameter and I based a lot of conversions with constructor parameter! What a news for me, I can't believe that every kind of object, except for primitive vairables such String, Boolean and Number, could be modified in this way: function MyObj(){}; var obj = new MyObj, str = new String("test"), sts = "test"; obj.constructor = Array; str.constructor = Array; sts.constructor = Array; alert([ obj.constructor === Array, str.constructor === Array, sts.constructor === Array ]); // true, true, false What does it me...

PHP 5 static keyword feature - Part 2

To explain better what did I mean when I talked about static keyword problems, here is some code examples (same result with different languages) C# (Console Application) using System; namespace ConsoleApplication1{ public class StaticString{ private String str; public StaticString(String str){ this.str = str; } public String getString(){ return str; } public void write(){ str += " from instance"; StaticString.write(this); } public static void write(StaticString what){ Console.WriteLine(what.getString()); } } class Program{ static void Main(string[] args){ StaticString ss = new StaticString("Hello World!"); StaticString.write(ss); // I have a static method to do something ss.write(); // and an instance method to do something else Console.ReadLine(); } } } ActionScript 2.0 class StaticString { // StaticString.as // generic public instance method public function getString(Void):String { return str; } // gen...

PHP 5 developers teach us what does static keyword mean (and this is a feature!)

PHP 5 introduced a "new" keyword called static . You can read here what does this keyword mean when it's applied to one method. As You can read in documentation page Declaring class members or methods as static makes them accessible without needing an instantiation of the class . A member declared as static can not be accessed with an instantiated class object ( though a static method can ). They put last tiny point inside brackets as it's not a really important point to analize! As You know, with PHP4 every method should be used as static one. The difference between PHP 4 and PHP 5 seems to be this one: The big difference between php 4 and php 5 is that a method declared as "static" does not have $this set. You'll get a fatal error, in fact, if you try to use $this in a static method. Well ... static method cannot have a $this reference inside its scope but every static method is inherited into every instance ... this is absolutely hilarious: You ca...

for in loop prototype safe

In many different libraries there should be one or more prototype method for every native JavaScript constructor. Every time You do a for in loop for(var key in object) ... You probably need to check object[key]. Add prototype to Object constructor is never a good practices, but for in loop can be used with other native constructors too, as example, Array. Array.prototype.randomValue = function(){ return this[Math.floor(Math.random() * this.length)]; }; var myArray = [1, 2, 3]; alert(myArray.randomValue()); // 1 or 2 or 3 The common way to loop over an array is a loop that uses index integer values but sometime You should need to loop over a generic variable just using a for in loop. for(var key in myArray) { // dostuff } In this case the "randomValue" key is part of loop but You can't know everytime wich library add its Array prototype so this is my simple proposal, the $for function. function $for(obj, callback){ var proto = obj.constructor.prototype, h = o...

Ultra Zipped Bitmap - a new (un)probable image standard?

Microsoft is working on a new image format, called HD . It seems to be better than standard JPEG format but it's not a loosequality format. I've done some strange and stupid test to find a way to obtain the better quality/size effort for an image, and this is the result: uzb image ... what's that? :D It's quite a joke but it should be a proposal for FireFox, Opera or other browser plugin (is it ridiculous?) The original uzb image format is Bitmap , that's a looseless image type but it's an "old" format and generated size is amazing. But, for example, this is an 885 Kbytes ... how many bytes do You think I removed? Exactly 829,1 Kbytes for a final result of 55,9 Kbytes !!! This "incredible" final size was obtained using fantastic LZMA compression Algorithm and this size si even better than every png and, as png format, is looseless! This is what i think about uzb: Features - looseless - extremely fast decompression - best quality/size...