Posts

Showing posts from March, 2007

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

function or var function ? part #2

I did an error talking about difference using function, var function and (function), because as Matteo said in my last post I didn't explain perfeclty the difference from an anonymous function and a simple reference variable. Here You can view an example, probably better than every word: var // temporary scope variable declaration myfunc // variable name (as function referer) = function(){}; // anonymous function alert(myfunc.name); // empty string because myfunc // is a reference of an // anonymous function // ---------------------------- // function // temporary scope function declaration myotherfunc(){};// "the" myotherfunc function alert(myotherfunc.name); // string "myotherfunc" because // myotherfunc is not a reference // but exactly a function So, an anonymous function has some limit, as I said on my old post but if You need a simple, tiny function that you're not using as class, You could declare anonymous function even inside a for loop for(var

Unobtrusive Blog Entry Date

I really like this Brainstorms Raves blog CSS based entry date tutorial but I like unobtrusive content and graceful enanchemet too :) That's why I created a simple example page to show You how to create a better unobtrusive example, using less tags on page and a bit of JavaScript for compatible browsers (I suppose every recent browser should support that code). You could view directly CSS or JavaScript source code, do You like it?

Five (better) Javascript Tools Someone Should Have ...

Sometimes there's a JavaScripter that " teach us " wich function should be on our Top Ten function list. My first disappointment is that if You use a good library, such jQuery , Dojo , !YUI or MooTools You'll never need these functions because these library probably will have better or more powerful implementation. However, if You absolutely need a " Top Ten " I think it should be created using best version of each function or prototype (write once and forget them). Please Read JS manual and Respect Standards This is a common javascripter error, he probably doesn't know well each official method so he thinks we need its code implementation. For example the common find Array prototype is, at least in my opinion, a big error because JavaScript 1.5 has its dedicated standard function to do the same thing. We don't need another prototype to do exactly the same thing of standard function, but if we need a different version we should create a little pe

Working on Byte Framework ...

Byte What? He he :-) .... I'm working on a framework that will include every interesting personal experiment such bytefx , byteson , bytedom and many others as JSL , Astar for game development, something from JHP , some component , some JS/Canvas/Flash interaction like PixelFont and more and more ... that's why this framework will be probably ready at the end of 2007, at least I hope because as You know, time is never enought. This post is only a preview of my work in progress and it includes: a first Tween class demo a stupid Css clas demo a Bpath (Base Xpath) benchmark against jQuery, Dojo and DOMQuery a Bpath compatibility demostration ... and a work in progress documentation page. Finally, these are some interesting features about this project: Compatible with IE 5 or greater full compatibility with IE 5.5 or greater better FX implementation clear source code, parsed and cached directly with my GzOutput class cross-browser and quality code for a fast framework unobtru

[IT] Italia - Finalmente i conti tornano - Atto Secondo

Ancora sul portale del turismo? Assolutamente no, semplicemente una ulteriore riflessione sulle competenze generali in ambito informatico e nello specifico Web di questo bel paese. Solo oggi scopro la notizia delle Olimpiadi Informatiche e per la prima volta mi ritrovo nel sito dell'evento . Mentre tutto il mondo parla di Web 2.0 in Italia la situazione è da Web 0.0.1 Alpha. Se le potenzialità tecniche e teoriche dei cervelli italiani sono riconosciute a livello internazionale come di altissimo livello quelle inerenti la progettazione Web farebbero rabbrividire anche i paesi che si sono affacciati alla New Economy solo da un paio di giorni. nessuna dichiarazione del tipo di documento, non sappiamo nemmeno cosa sia il DocType utilizzo preistorico di più frame per un sito graficamente discutibile utilizzo di applicativi degni di un webmaster al primo giorno di studi (sito realizzato con Microsoft FrontPage 5.0) tabelle ed orrori tipici di questi software da principiante amatoriale de

DOM Google Translator

Hi guys :-) I've just uploaded my last function on devpro that's compatible with most common browser. It's based on DOM and creates automatically translation request url to view a selected text or an entire page inside wonderful Google Translator free service page. Its usage is really simple: onload = function(){ document.body.appendChild( GoogleTranslator() ); }; With these example You'll append at the end of document.body a select with LANG - TRANSLATION values and a link. Returned div has exactly these two elements and You could customize them using your own CSS. Here You can view another example <style type="text/css"> #mytranslator { font-size: 8pt; font-family: Verdana, Helvetica, sans-serif; border: 1px solid silver; padding: 4px; background-color: #F5F5F5; color: #000; width: 160px; } #mytranslator select { font-size: 8pt; } #mytranslator a { display: block; color: #00F; text-decoration: none; border-bottom: 1px dotted black; }

function, (function) or var function?

Few days ago Dustin Diaz wrote about function declaration ambiguity. In my opinion the best way to declare a function (class) is the classic way function MyClass(){ // constructor }; The reason is simple: a class could be a very big function and some version of Explorer could generate an error if big function is declared using var MyClass = function (){ // constructor }; However this post talks about other function "strange things", expecially about usage of brackets after function declaration. function MyTest(){alert(arguments.length)}(); This will produce an error, exactly a Syntax error. The "strange thing" is that You could do something inside brackets then function will not be called and syntax error will disappear. function MyTest(){alert(arguments.length)} (alert(MyTest)); This is a strange "feature" and it's different from this one: (function MyTest(){alert(arguments.length)}) (alert(MyTest)); This code will produce a reference error because