<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3707838969303802155</id><updated>2011-04-21T15:56:03.599-07:00</updated><title type='text'>Andrew Paluba</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://andrewpaluba.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://andrewpaluba.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Andrew</name><uri>http://www.blogger.com/profile/14938351567568650985</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3707838969303802155.post-7976522140300058210</id><published>2007-07-25T16:42:00.001-07:00</published><updated>2007-07-25T16:42:30.049-07:00</updated><title type='text'>anonymous function prototype</title><content type='html'>There are several scripts that use anonymous functions and we often use them as regulars.&lt;br /&gt;Is there a way to have different or dedicated prototypes for this kind of function ?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 1, what is anonymous function ?&lt;/strong&gt;&lt;br /&gt;The "original" anonymous function is returned from the global Function object.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt;    myFunc &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;(&lt;span class="jshstrings"&gt;"a"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"b"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"c"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"return a + b + c;"&lt;/span&gt;);&lt;br /&gt;alert(myFunc(&lt;span class="jshnumbers"&gt;1&lt;/span&gt;,&lt;span class="jshnumbers"&gt;2&lt;/span&gt;,&lt;span class="jshnumbers"&gt;3&lt;/span&gt;));    &lt;span class="jshcomments"&gt;// number 6&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;myFunc is a function with all Function prototypes or native methods and with the same constructor of a function.&lt;br /&gt;Then myFunc is absolutely a function, but wich kind of function is it ? Exactly this one:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; anonymous(a, b, c) {&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; a &lt;span class="jshoperators"&gt;+&lt;/span&gt; b &lt;span class="jshoperators"&gt;+&lt;/span&gt; c;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What's that ? That's a function that exists but you can't modify, get, or extend anyway because each new Function will produce a different referer for a different anonymous function.&lt;br /&gt;&lt;pre class="code"&gt;(&lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;).&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.isAnonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; true;&lt;br /&gt;alert((&lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;).isAnonymous);    &lt;span class="jshcomments"&gt;// undefined&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;You can try in a different ways creating for example a personal anonymous function, but the result will be the same ...&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt; anonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;;&lt;br /&gt;(&lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;).&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.isAnonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; true;&lt;br /&gt;alert((&lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;).isAnonymous);    &lt;span class="jshcomments"&gt;// undefined&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// other way&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; anonymous(){};&lt;br /&gt;anonymous.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.isAnonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; true;&lt;br /&gt;alert((&lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;).isAnonymous);    &lt;span class="jshcomments"&gt;// undefined&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;That's becaus, as I've just said, every anonymous instance is different from every other.&lt;br /&gt;With FireFox you should view this difference using toSource Obect native method.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; anonymous(){};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt; realanonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;alert([&lt;br /&gt;   anonymous.&lt;span class="jshmethods"&gt;toSource&lt;/span&gt;(),        &lt;span class="jshcomments"&gt;// function anonymous() {}&lt;br /&gt;&lt;/span&gt;    realanonymous.&lt;span class="jshmethods"&gt;toSource&lt;/span&gt;(),    &lt;span class="jshcomments"&gt;// (function anonymous() {})&lt;br /&gt;&lt;/span&gt;    anonymous &lt;span class="jshoperators"&gt;===&lt;/span&gt; realanonymous    &lt;span class="jshcomments"&gt;// false&lt;br /&gt;&lt;/span&gt;]);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As you can see, those parentheses are the key to understand the anonymous function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 2, parentheses and virtual scope&lt;/strong&gt;&lt;br /&gt;At this point we know that an anonymous functions cannot have dedicated prototypes and aren't like regular functions too.&lt;br /&gt;In some script you can see the use of parentheses to call runtime a function or to create one.&lt;br /&gt;This code, for example, is an unobtrusive way to add a personal String prototype.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;String&lt;/span&gt;.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.toArray &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;span class="jshglobals"&gt;function&lt;/span&gt;(base){&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; base || &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){&lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;this&lt;/span&gt;.&lt;span class="jshmethods"&gt;split&lt;/span&gt;(&lt;span class="jshstrings"&gt;""&lt;/span&gt;)}&lt;br /&gt;})(&lt;span class="jshglobals"&gt;String&lt;/span&gt;.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.toArray);&lt;br /&gt;&lt;br /&gt;alert(&lt;span class="jshstrings"&gt;"test"&lt;/span&gt;.toArray());    &lt;span class="jshcomments"&gt;// t,e,s,t&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Let me explain this few lines of code.&lt;br /&gt;If some script, before this one, has just defined a String.prototype.toArray or browser has a native String.toArray function, the anonymous function created using parentheses and directly called with (String.prototype.toArray) that accepts, if present, the base function, will assign to that prototype old version (base) or, if base is not defind, our prototype function (function(){return this.split("")}).&lt;br /&gt;The closed anonymous function is then a special function and its really usefull to solve a lot of problems.&lt;br /&gt;This is only a little example but I think you used anonymous functions every day with every scripts ;)&lt;br /&gt;Since the scope inside parentheses "magically disappear", but neither for itsself nor for its internal scope, we can think that those kind of functions are exactly anonymous.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt;    myAnonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;span class="jshglobals"&gt;function&lt;/span&gt; anonymous(){}),&lt;br /&gt;   realAnonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;span class="jshglobals"&gt;new&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;alert([&lt;br /&gt;   myAnonymous,            &lt;span class="jshcomments"&gt;// (function anonymous(){})&lt;br /&gt;&lt;/span&gt;    realAnonymous,            &lt;span class="jshcomments"&gt;// (function anonymous(){})&lt;br /&gt;&lt;/span&gt;    myAnonymous &lt;span class="jshoperators"&gt;===&lt;/span&gt; realAnonymous,    &lt;span class="jshcomments"&gt;// false&lt;br /&gt;&lt;/span&gt;    myAnonymous.&lt;span class="jshmethods"&gt;toSource&lt;/span&gt;() &lt;span class="jshoperators"&gt;===&lt;/span&gt; realAnonymous.&lt;span class="jshmethods"&gt;toSource&lt;/span&gt;()&lt;br /&gt;                   &lt;span class="jshcomments"&gt;// true !!!&lt;br /&gt;&lt;/span&gt;]);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 3, how to create a dedicate prototype&lt;/strong&gt;&lt;br /&gt;JavaScript is Object Oriented and each function is an object, then why I couldn't use "special" anonymous functions as an object ?&lt;br /&gt;That's why I've created a simple solution to have customizable anonymous functions, every one will be different from every other, but everyone will have our dedicate prototypes.&lt;br /&gt;This is the concept function&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshcomments"&gt;// anonymous explicit function&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; anonymous() {&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// prototype to prototype,&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshcomments"&gt;//    this function copy each a prototype (p) to other (b) function&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;function&lt;/span&gt; p2p(p,a,b) {&lt;br /&gt; &lt;br /&gt;       &lt;span class="jshcomments"&gt;// using prototype for b too isn't a good solution (imho)&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// because only new anonymous will has these prototypes&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshglobals"&gt;for&lt;/span&gt;(&lt;span class="jshglobals"&gt;var&lt;/span&gt; k &lt;span class="jshglobals"&gt;in&lt;/span&gt; a[p])b[k] &lt;span class="jshoperators"&gt;=&lt;/span&gt; a[p][k];&lt;br /&gt;       &lt;span class="jshglobals"&gt;return&lt;/span&gt; b;&lt;br /&gt;   };&lt;br /&gt; &lt;br /&gt;   &lt;span class="jshcomments"&gt;// we need arguments and its length plus a genric array&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;var&lt;/span&gt;    l &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;.&lt;span class="jshproperties"&gt;length&lt;/span&gt;, a &lt;span class="jshoperators"&gt;=&lt;/span&gt; [];&lt;br /&gt; &lt;br /&gt;   &lt;span class="jshcomments"&gt;// if argument is not one or its not a function&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;if&lt;/span&gt;(l &lt;span class="jshoperators"&gt;!==&lt;/span&gt; &lt;span class="jshnumbers"&gt;1&lt;/span&gt; || &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;].&lt;span class="jshproperties"&gt;constructor&lt;/span&gt; &lt;span class="jshoperators"&gt;!==&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;) {&lt;br /&gt; &lt;br /&gt;       &lt;span class="jshcomments"&gt;// create the string ([arguments[N],...,arguments[0]])&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshglobals"&gt;while&lt;/span&gt;(l)a.&lt;span class="jshmethods"&gt;push&lt;/span&gt;(&lt;span class="jshstrings"&gt;"arguments["&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(&lt;span class="jshoperators"&gt;--&lt;/span&gt;l,&lt;span class="jshstrings"&gt;"]"&lt;/span&gt;));&lt;br /&gt;     &lt;br /&gt;       &lt;span class="jshcomments"&gt;// then reverse ...&lt;br /&gt;&lt;/span&gt;        a.&lt;span class="jshmethods"&gt;reverse&lt;/span&gt;();&lt;br /&gt;     &lt;br /&gt;       &lt;span class="jshcomments"&gt;// ... to assign anonymous function to arguments 0&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshglobals"&gt;&lt;span class="jshmethods"&gt;eval&lt;/span&gt;&lt;/span&gt;(&lt;span class="jshstrings"&gt;"arguments[0]=new Function("&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(a.&lt;span class="jshmethods"&gt;join&lt;/span&gt;(&lt;span class="jshstrings"&gt;","&lt;/span&gt;),&lt;span class="jshstrings"&gt;")"&lt;/span&gt;));&lt;br /&gt;   }&lt;br /&gt; &lt;br /&gt;   &lt;span class="jshcomments"&gt;// return its "prototyped" version of anonymous function&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;return&lt;/span&gt; p2p(&lt;span class="jshstrings"&gt;"prototype"&lt;/span&gt;, &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;.callee, &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;]);&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The major difference from native new Function(arguments) is that my anonymous function accpets an anonymous function too.&lt;br /&gt;Here there's a complete test to view some application.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshcomments"&gt;// anonymous explicit function&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; anonymous() {&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt; p2p(p,a,b) {&lt;br /&gt;       &lt;span class="jshglobals"&gt;for&lt;/span&gt;(&lt;span class="jshglobals"&gt;var&lt;/span&gt; k &lt;span class="jshglobals"&gt;in&lt;/span&gt; a[p])b[k] &lt;span class="jshoperators"&gt;=&lt;/span&gt; a[p][k];&lt;br /&gt;       &lt;span class="jshglobals"&gt;return&lt;/span&gt; b;&lt;br /&gt;   };&lt;br /&gt;   &lt;span class="jshglobals"&gt;var&lt;/span&gt;    l &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;.&lt;span class="jshproperties"&gt;length&lt;/span&gt;, a &lt;span class="jshoperators"&gt;=&lt;/span&gt; [];&lt;br /&gt;   &lt;span class="jshglobals"&gt;if&lt;/span&gt;(l &lt;span class="jshoperators"&gt;!==&lt;/span&gt; &lt;span class="jshnumbers"&gt;1&lt;/span&gt; || &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;].&lt;span class="jshproperties"&gt;constructor&lt;/span&gt; &lt;span class="jshoperators"&gt;!==&lt;/span&gt; &lt;span class="jshglobals"&gt;Function&lt;/span&gt;) {&lt;br /&gt;       &lt;span class="jshglobals"&gt;while&lt;/span&gt;(l)a.&lt;span class="jshmethods"&gt;push&lt;/span&gt;(&lt;span class="jshstrings"&gt;"arguments["&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(&lt;span class="jshoperators"&gt;--&lt;/span&gt;l,&lt;span class="jshstrings"&gt;"]"&lt;/span&gt;));&lt;br /&gt;       a.&lt;span class="jshmethods"&gt;reverse&lt;/span&gt;();&lt;br /&gt;       &lt;span class="jshglobals"&gt;&lt;span class="jshmethods"&gt;eval&lt;/span&gt;&lt;/span&gt;(&lt;span class="jshstrings"&gt;"arguments[0]=new Function("&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(a.&lt;span class="jshmethods"&gt;join&lt;/span&gt;(&lt;span class="jshstrings"&gt;","&lt;/span&gt;),&lt;span class="jshstrings"&gt;")"&lt;/span&gt;));&lt;br /&gt;   }&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; p2p(&lt;span class="jshstrings"&gt;"prototype"&lt;/span&gt;, &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;.callee, &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;]);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// common Function prototype&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;Function&lt;/span&gt;.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.isFunction &lt;span class="jshoperators"&gt;=&lt;/span&gt; true;&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// only anonymous prototype&lt;br /&gt;&lt;/span&gt;anonymous.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.isAnonymous &lt;span class="jshoperators"&gt;=&lt;/span&gt; true;&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// first test --------------------------------------&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;//    new anonymous creation with the same sintax of new Function&lt;br /&gt;&lt;/span&gt;&lt;span class="jshmethods"&gt;test&lt;/span&gt; &lt;span class="jshoperators"&gt;=&lt;/span&gt; anonymous(&lt;span class="jshstrings"&gt;"str"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"alert(str)"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// just few checks&lt;br /&gt;&lt;/span&gt;alert([&lt;br /&gt;   &lt;span class="jshstrings"&gt;""&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; anonymous.isAnonymous,    &lt;span class="jshcomments"&gt;// undefined, anonymous is a function&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshstrings"&gt;""&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshmethods"&gt;test&lt;/span&gt;.isFunction,        &lt;span class="jshcomments"&gt;// true, test is a function&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshstrings"&gt;""&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshmethods"&gt;test&lt;/span&gt;.isAnonymous        &lt;span class="jshcomments"&gt;// true, test is an anonymous function&lt;br /&gt;&lt;/span&gt;]);&lt;br /&gt;&lt;br /&gt;&lt;span class="jshmethods"&gt;test&lt;/span&gt;(&lt;span class="jshstrings"&gt;"Hello World!"&lt;/span&gt;);            &lt;span class="jshcomments"&gt;// Hello World! [then test works perfectly]&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;// _________________________________________________&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// second test --------------------------------------&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;//    common function declaration using anonymous&lt;br /&gt;&lt;/span&gt;&lt;span class="jshmethods"&gt;test&lt;/span&gt; &lt;span class="jshoperators"&gt;=&lt;/span&gt; anonymous(&lt;span class="jshglobals"&gt;function&lt;/span&gt;(str){alert(str.&lt;span class="jshmethods"&gt;toUpperCase&lt;/span&gt;())});&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// just check it&lt;br /&gt;&lt;/span&gt;alert([&lt;br /&gt;   &lt;span class="jshstrings"&gt;""&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshmethods"&gt;test&lt;/span&gt;.isFunction,        &lt;span class="jshcomments"&gt;// true, test is a function&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshstrings"&gt;""&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshmethods"&gt;test&lt;/span&gt;.isAnonymous        &lt;span class="jshcomments"&gt;// true, test is an anonymous function&lt;br /&gt;&lt;/span&gt;]);&lt;br /&gt;&lt;br /&gt;&lt;span class="jshmethods"&gt;test&lt;/span&gt;(&lt;span class="jshstrings"&gt;"Hello World!"&lt;/span&gt;);            &lt;span class="jshcomments"&gt;// HELLO WORLD! [then test works perfectly]&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;// _________________________________________________&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// third test --------------------------------------&lt;br /&gt;&lt;/span&gt;alert(anonymous(&lt;span class="jshglobals"&gt;function&lt;/span&gt;(){}).isAnonymous);    &lt;span class="jshcomments"&gt;// true&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;// _________________________________________________&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// final test --------------------------------------&lt;br /&gt;&lt;/span&gt;anonymous(&lt;span class="jshglobals"&gt;function&lt;/span&gt;(a){alert(a &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;.callee.isAnonymous)})(&lt;span class="jshstrings"&gt;"Anonymous ? "&lt;/span&gt;);&lt;br /&gt;                       &lt;span class="jshcomments"&gt;// true&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;// _________________________________________________&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Just a look at the last test, where is used arguments.callee instead of "this".&lt;br /&gt;That's simply why the "this doesn't exists" inside the function ("this" inside a function is the window object).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3707838969303802155-7976522140300058210?l=andrewpaluba.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewpaluba.blogspot.com/feeds/7976522140300058210/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3707838969303802155&amp;postID=7976522140300058210' title='0 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/7976522140300058210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/7976522140300058210'/><link rel='alternate' type='text/html' href='http://andrewpaluba.blogspot.com/2007/07/anonymous-function-prototype.html' title='anonymous function prototype'/><author><name>Andrew</name><uri>http://www.blogger.com/profile/14938351567568650985</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3707838969303802155.post-6379464867042591403</id><published>2007-07-25T16:40:00.000-07:00</published><updated>2007-07-25T16:41:29.322-07:00</updated><title type='text'>anonymous function to add simple events</title><content type='html'>This post shows a simple anonymous function application example, a fake addEventListener.&lt;br /&gt;We often use directly the method with the document element, it's simple and fast, then often our preferred way to implement an event.&lt;br /&gt;I'm talking about this code&lt;br /&gt;&lt;pre class="code"&gt;document.getElementById(&lt;span class="jshstrings"&gt;"myId"&lt;/span&gt;).onclick &lt;span class="jshoperators"&gt;=&lt;/span&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"hello"&lt;/span&gt;)};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;DOM and standars like this method to add an event&lt;br /&gt;&lt;pre class="code"&gt;document.getElementById(&lt;span class="jshstrings"&gt;"myId"&lt;/span&gt;).addEventListener(&lt;br /&gt;   &lt;span class="jshstrings"&gt;"click"&lt;/span&gt;,&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"hello"&lt;/span&gt;)},&lt;br /&gt;   false&lt;br /&gt;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;But IE doesn't implement addEventListener method (what a news ...)&lt;br /&gt;&lt;pre class="code"&gt;document.getElementById(&lt;span class="jshstrings"&gt;"myId"&lt;/span&gt;).attachEvent(&lt;br /&gt;   &lt;span class="jshstrings"&gt;"onclick"&lt;/span&gt;,&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"hello"&lt;/span&gt;)}&lt;br /&gt;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The attachEvent has different problems, it isn't a standard method (then IE and few other browsers supports that) and the scope inside the callback is not the element.&lt;br /&gt;This code, for example, doesn't work as expected:&lt;br /&gt;&lt;pre class="code"&gt;document.getElementById(&lt;span class="jshstrings"&gt;"myId"&lt;/span&gt;).attachEvent(&lt;br /&gt;   &lt;span class="jshstrings"&gt;"onclick"&lt;/span&gt;,&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshglobals"&gt;this&lt;/span&gt;.&lt;span class="jshproperties"&gt;className&lt;/span&gt;)}&lt;br /&gt;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I've implemented the addEventListener in my &lt;a href="http://webreflection.blogspot.com/2006/09/blogspot-modified-my-library.html"&gt;&lt;/a&gt;big dollar function but often developers doesn't like "big" external libraries (scriptacolous as prototype and Dojo are some exceptions).&lt;br /&gt;That's why I'm writing this simple function to add an event directly to an element, using generic on* event names.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; addSimpleEvent(&lt;br /&gt;       obj,        &lt;span class="jshcomments"&gt;// the object (i.e. window, document, element)&lt;br /&gt;&lt;/span&gt;        type,        &lt;span class="jshcomments"&gt;// the type (i.e. "onload", "onmouseover", "onclick")&lt;br /&gt;&lt;/span&gt;        callback    &lt;span class="jshcomments"&gt;// the callback (i.e. function(){alert(this)})&lt;br /&gt;&lt;/span&gt;    ) {&lt;br /&gt;   obj[type] &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;span class="jshglobals"&gt;function&lt;/span&gt;(base){            &lt;span class="jshcomments"&gt;// anonymous function&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;function&lt;/span&gt;(evt){            &lt;span class="jshcomments"&gt;// function called on event&lt;br /&gt;&lt;/span&gt;            &lt;span class="jshglobals"&gt;if&lt;/span&gt;(&lt;span class="jshoperators"&gt;!&lt;/span&gt;evt)evt&lt;span class="jshoperators"&gt;=&lt;/span&gt;window.event;    &lt;span class="jshcomments"&gt;// event for IE browsers&lt;br /&gt;&lt;/span&gt;            &lt;span class="jshglobals"&gt;if&lt;/span&gt;(base)base.&lt;span class="jshmethods"&gt;call&lt;/span&gt;(&lt;span class="jshglobals"&gt;this&lt;/span&gt;,evt);    &lt;span class="jshcomments"&gt;// old function, if defined&lt;br /&gt;&lt;/span&gt;            callback.&lt;span class="jshmethods"&gt;call&lt;/span&gt;(&lt;span class="jshglobals"&gt;this&lt;/span&gt;,evt);    &lt;span class="jshcomments"&gt;// callback&lt;br /&gt;&lt;/span&gt;        }&lt;br /&gt;   })(obj[type])                    &lt;span class="jshcomments"&gt;// old defined (or not) function&lt;br /&gt;&lt;/span&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This function uses anonymous function to preserve old event (base variable), if presents, and calls every callback with the element scope (using call).&lt;br /&gt;This is an example:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshcomments"&gt;// imagine that other script did it ...&lt;br /&gt;&lt;/span&gt;onload &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"Hello 1"&lt;/span&gt;)};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// addSimpleEvent function&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; addSimpleEvent(obj,type,callback) {&lt;br /&gt;   obj[type] &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;span class="jshglobals"&gt;function&lt;/span&gt;(base){&lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;function&lt;/span&gt;(evt){&lt;br /&gt;       &lt;span class="jshglobals"&gt;if&lt;/span&gt;(&lt;span class="jshoperators"&gt;!&lt;/span&gt;evt)evt&lt;span class="jshoperators"&gt;=&lt;/span&gt;window.event;&lt;br /&gt;       &lt;span class="jshglobals"&gt;if&lt;/span&gt;(base)base.&lt;span class="jshmethods"&gt;call&lt;/span&gt;(&lt;span class="jshglobals"&gt;this&lt;/span&gt;,evt);&lt;br /&gt;       callback.&lt;span class="jshmethods"&gt;call&lt;/span&gt;(&lt;span class="jshglobals"&gt;this&lt;/span&gt;,evt);&lt;br /&gt;   }})(obj[type])&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// you can add one, two or more events&lt;br /&gt;&lt;/span&gt;addSimpleEvent(window, &lt;span class="jshstrings"&gt;"onload"&lt;/span&gt;, &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"Hello 2"&lt;/span&gt;)});&lt;br /&gt;addSimpleEvent(window, &lt;span class="jshstrings"&gt;"onload"&lt;/span&gt;, &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"Hello 3"&lt;/span&gt;)});&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// if you want, you could create another function to add multiple events of same type&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; addMultipleEvents() {&lt;br /&gt;   &lt;span class="jshglobals"&gt;for&lt;/span&gt;(&lt;span class="jshglobals"&gt;var&lt;/span&gt; i &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;2&lt;/span&gt;, j &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;.&lt;span class="jshproperties"&gt;length&lt;/span&gt;; i &lt; class="jshoperators"&gt;++&lt;/span&gt;)&lt;br /&gt;       addSimpleEvent(&lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;], &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[&lt;span class="jshnumbers"&gt;1&lt;/span&gt;], &lt;span class="jshproperties"&gt;arguments&lt;/span&gt;[i]);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// and use it in this way&lt;br /&gt;&lt;/span&gt;addMultipleEvents(window, &lt;span class="jshstrings"&gt;"onload"&lt;/span&gt;,&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"Hello 4"&lt;/span&gt;)},&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"Hello 5"&lt;/span&gt;)},&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){alert(&lt;span class="jshstrings"&gt;"Hello 6"&lt;/span&gt;)}&lt;br /&gt;);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Just test this script to view this sequence of alerts&lt;br /&gt;&lt;br /&gt;Hello 1&lt;br /&gt;Hello 2&lt;br /&gt;Hello 3&lt;br /&gt;Hello 4&lt;br /&gt;Hello 5&lt;br /&gt;Hello 6&lt;br /&gt;&lt;br /&gt;What's about compatibility ? Every browser that supports &lt;strong&gt;call&lt;/strong&gt; and doesn't loose base variable during execution, then IE 5.5+, FireFox, Opera, KDE, Safari and many others.&lt;br /&gt;&lt;br /&gt;I hope this little function will be usefull for you window, document, or element common events.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3707838969303802155-6379464867042591403?l=andrewpaluba.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewpaluba.blogspot.com/feeds/6379464867042591403/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3707838969303802155&amp;postID=6379464867042591403' title='0 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/6379464867042591403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/6379464867042591403'/><link rel='alternate' type='text/html' href='http://andrewpaluba.blogspot.com/2007/07/anonymous-function-to-add-simple-events.html' title='anonymous function to add simple events'/><author><name>Andrew</name><uri>http://www.blogger.com/profile/14938351567568650985</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3707838969303802155.post-6665153619784868230</id><published>2007-07-25T16:39:00.002-07:00</published><updated>2007-07-25T16:40:23.346-07:00</updated><title type='text'>Are speed tests really usefull for developers</title><content type='html'>Yesterday my favourite portal HTML.it has posted a speed test inside its blog.&lt;br /&gt;This JavaScript benchmark was posted on this site.&lt;br /&gt;&lt;br /&gt;I think that those kind of tests are only a "point of view" and aren't comparable with real javascript usage.&lt;br /&gt;As I've showed on my for and while loop test, every operation is a single case to analize and every single case has not everytime the same result.&lt;br /&gt;&lt;br /&gt;For example using a for and an &lt;= expression with every loop should be faster in some browser and should be slower in some other.&lt;br /&gt;Then it's not always a good way to compare some internal loop operations because a browser should be faster to push a string inside an array but should be slower to execute that kind of for loop.&lt;br /&gt;&lt;br /&gt;Often speed depends on garbage collector too, that should be faster in some point of execution and slower in some other.&lt;br /&gt;&lt;br /&gt;These concept are true with every other benchmark of that site.&lt;br /&gt;&lt;br /&gt;Math engine, as Ajax declaration, are faster with FireFox and really bad with IE because the object has a dedicated try catch for every IE browser.&lt;br /&gt;I usually don't use that way to declare an XMLHttpRequest then my library should be faser than Opera with IE too.&lt;br /&gt;If you change some piece of execution code, you can see that IE6 or 7 is even slower than FireFox with some function.&lt;br /&gt;&lt;br /&gt;What I mean is that a real "every day" application will "never" use 4000 try catch but should use a lot of Math operations and DOM elements using many array and Ajax declarations too.&lt;br /&gt;Every Ajax object or class should be simple or really complex and some browser should declare it faster or slower than other one.&lt;br /&gt;&lt;br /&gt;Another generic relevant thing is code optimizzation ... you could use "only Opera" to surf the web but if a library contains bad practices its code execution will be slow in every case.&lt;br /&gt;&lt;br /&gt;And again, the use of "var" before every temporary variable should be slower or faster, look at the MathEngine function ... is this usefull ?&lt;br /&gt;is this a real application example ?&lt;br /&gt;&lt;br /&gt;The StringFuncs has 2 operations that are only for human eyes because if you don't call those functions how can you test them ?&lt;br /&gt;&lt;pre class="code"&gt;str.&lt;span class="jshmethods"&gt;toLowerCase&lt;/span&gt;;    &lt;span class="jshcomments"&gt;// yeah! a string has a toLowerCase prototype ..&lt;br /&gt;&lt;/span&gt;str.&lt;span class="jshmethods"&gt;toUpperCase&lt;/span&gt;;    &lt;span class="jshcomments"&gt;// yeah! a string has a toUpperCase prototype ..&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// P.S. I think that He's just forgot it :)&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;A "StringBuilder" function should be a better test too because every JS developer knows that using string += something isn't a good speed practice and the use of concat instead of '+''+''+''+''+' ... is faster too.&lt;br /&gt;A StringBuilder operation is not different from array.push, but doesn't use sort and reverse as ArrayFuncs does.&lt;br /&gt;&lt;br /&gt;Is a StringBuilred competition another usefull test to do ? I think that ...&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; StringBuilder() {&lt;br /&gt;   &lt;span class="jshglobals"&gt;function&lt;/span&gt; StringBuilder() {&lt;br /&gt;       &lt;span class="jshglobals"&gt;this&lt;/span&gt;.append &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;function&lt;/span&gt;(what){&lt;br /&gt;           arr.&lt;span class="jshmethods"&gt;push&lt;/span&gt;(what);&lt;br /&gt;       };&lt;br /&gt;       &lt;span class="jshglobals"&gt;this&lt;/span&gt;.&lt;span class="jshmethods"&gt;toString&lt;/span&gt; &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;function&lt;/span&gt;(){&lt;br /&gt;           &lt;span class="jshglobals"&gt;return&lt;/span&gt; arr.&lt;span class="jshmethods"&gt;join&lt;/span&gt;(&lt;span class="jshstrings"&gt;""&lt;/span&gt;);&lt;br /&gt;       };&lt;br /&gt;       &lt;span class="jshglobals"&gt;var&lt;/span&gt;    arr &lt;span class="jshoperators"&gt;=&lt;/span&gt; [&lt;span class="jshstrings"&gt;""&lt;/span&gt;];&lt;br /&gt;   };&lt;br /&gt;   &lt;span class="jshglobals"&gt;var&lt;/span&gt; startTime &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshmethods"&gt;getTime&lt;/span&gt;(), i &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;4001&lt;/span&gt;, sb &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;new&lt;/span&gt; StringBuilder;&lt;br /&gt;   &lt;span class="jshglobals"&gt;while&lt;/span&gt;(i&lt;span class="jshoperators"&gt;--&lt;/span&gt;)&lt;br /&gt;       sb.append(&lt;span class="jshstrings"&gt;"a piece of code"&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(i));&lt;br /&gt;   sb &lt;span class="jshoperators"&gt;=&lt;/span&gt; sb.&lt;span class="jshmethods"&gt;toString&lt;/span&gt;();&lt;br /&gt;   &lt;span class="jshglobals"&gt;var&lt;/span&gt; endTime &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshmethods"&gt;getTime&lt;/span&gt;();&lt;br /&gt;   getElement(&lt;span class="jshstrings"&gt;'speed_sb'&lt;/span&gt;).innerHTML &lt;span class="jshoperators"&gt;=&lt;/span&gt; (endTime &lt;span class="jshoperators"&gt;-&lt;/span&gt; startTime);&lt;br /&gt;   getElement(&lt;span class="jshstrings"&gt;'speed_total'&lt;/span&gt;).innerHTML &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;&lt;span class="jshmethods"&gt;eval&lt;/span&gt;&lt;/span&gt;(getElement(&lt;span class="jshstrings"&gt;'speed_total'&lt;/span&gt;).innerHTML) &lt;span class="jshoperators"&gt;+&lt;/span&gt; (endTime &lt;span class="jshoperators"&gt;-&lt;/span&gt; startTime);&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;... and Should we care about few milliseconds ?&lt;br /&gt;&lt;br /&gt;Then this is what I think: these speed tests are a must only for Rhino or SpiderMonkey developers because a javascript developer doesn't need to care about performances (in this way) ... He needs to care about generic code optimizzations using lightweight code implementations without redundant operations inside a cross-browser code.&lt;br /&gt;That's what a developer should care about, not dedicated speed test for this or that function.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3707838969303802155-6665153619784868230?l=andrewpaluba.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewpaluba.blogspot.com/feeds/6665153619784868230/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3707838969303802155&amp;postID=6665153619784868230' title='0 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/6665153619784868230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/6665153619784868230'/><link rel='alternate' type='text/html' href='http://andrewpaluba.blogspot.com/2007/07/are-speed-tests-really-usefull-for.html' title='Are speed tests really usefull for developers'/><author><name>Andrew</name><uri>http://www.blogger.com/profile/14938351567568650985</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3707838969303802155.post-8250094142918234652</id><published>2007-07-25T16:39:00.001-07:00</published><updated>2007-07-25T16:39:39.489-07:00</updated><title type='text'>Running with a Number</title><content type='html'>I think this post is only for basic JS developers and shows how a simple function or a simple operation should be wrote in different ways.&lt;br /&gt;In this specific case, I would talk about logic used for a single function while I was optimizing &lt;a href="http://webreflection.blogspot.com/2006/09/simple-effects-bytefx.html"&gt;&lt;/a&gt;bytefx.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;br /&gt;I've two numbers, x and y, I need to change x, adding or removing a "speed" value, while x is not equal to y.&lt;br /&gt;&lt;br /&gt;This simple problem has a lot of solutions. This is probably the simplest one:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; xRun2y(x, y, speed) {&lt;br /&gt;   &lt;span class="jshcomments"&gt;// check wich number is greater than other one&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &lt; y) {&lt;br /&gt;&lt;br /&gt;       &lt;span class="jshcomments"&gt;// ok, x is less than y ... then add speed&lt;br /&gt;&lt;/span&gt;        x &lt;span class="jshoperators"&gt;+=&lt;/span&gt; speed;&lt;br /&gt;&lt;br /&gt;       &lt;span class="jshcomments"&gt;// x can't be greater than y ...&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// x can be only less than y ... or equal&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &gt; y)&lt;br /&gt;           x &lt;span class="jshoperators"&gt;=&lt;/span&gt; y;    &lt;span class="jshcomments"&gt;// stop run&lt;br /&gt;&lt;/span&gt;    }&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// other case, x is greater than y&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;else&lt;/span&gt; &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &gt; y) {&lt;br /&gt;    &lt;br /&gt;       &lt;span class="jshcomments"&gt;// well, in this case we remove speed from x&lt;br /&gt;&lt;/span&gt;        x &lt;span class="jshoperators"&gt;-=&lt;/span&gt; speed;&lt;br /&gt;&lt;br /&gt;       &lt;span class="jshcomments"&gt;// but x can't be lower than y ... then ...&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &lt; y)&lt;br /&gt;           x &lt;span class="jshoperators"&gt;=&lt;/span&gt; y;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// we don't need to care about x == y&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshcomments"&gt;// just return x&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;return&lt;/span&gt; x;&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As you can see by yourself, this simple function could create a range of numbers from Nstart to Nend.&lt;br /&gt;This is an example:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshcomments"&gt;// from 10 to 0&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt;    start &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;10&lt;/span&gt;, end &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;0&lt;/span&gt;, speed &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;1&lt;/span&gt;,&lt;br /&gt;   arr &lt;span class="jshoperators"&gt;=&lt;/span&gt; [];&lt;br /&gt;&lt;span class="jshglobals"&gt;while&lt;/span&gt;(start &lt;span class="jshoperators"&gt;!==&lt;/span&gt; end) {&lt;br /&gt;   start &lt;span class="jshoperators"&gt;=&lt;/span&gt; xRun2y(start, end, speed);&lt;br /&gt;   arr.&lt;span class="jshmethods"&gt;push&lt;/span&gt;(start);&lt;br /&gt;};&lt;br /&gt;alert(arr);    &lt;span class="jshcomments"&gt;// 9,8,7,6,5,4,3,2,1,0&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// from 0 to 10&lt;br /&gt;&lt;/span&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt;    start &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;0&lt;/span&gt;, end &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;10&lt;/span&gt;, speed &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;1&lt;/span&gt;,&lt;br /&gt;   arr &lt;span class="jshoperators"&gt;=&lt;/span&gt; [];&lt;br /&gt;&lt;span class="jshglobals"&gt;while&lt;/span&gt;(start &lt;span class="jshoperators"&gt;!==&lt;/span&gt; end) {&lt;br /&gt;   start &lt;span class="jshoperators"&gt;=&lt;/span&gt; xRun2y(start, end, speed);&lt;br /&gt;   arr.&lt;span class="jshmethods"&gt;push&lt;/span&gt;(start);&lt;br /&gt;};&lt;br /&gt;alert(arr);    &lt;span class="jshcomments"&gt;// 1,2,3,4,5,6,7,8,9,10&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;These operations should be usefull to move an element, to change some value from a startPoint to endPoint ... but, as i've said, there are different ways to do that.&lt;br /&gt;&lt;br /&gt;This way is, for me, a better way to write the same function.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; xRun2y(x, y, speed) {&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// check wich number is greater than other one&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &lt; y)&lt;br /&gt;&lt;br /&gt;       &lt;span class="jshcomments"&gt;// with a ternary operator we can do everything inline&lt;br /&gt;&lt;/span&gt;        x &lt;span class="jshoperators"&gt;=&lt;/span&gt; x &lt;span class="jshoperators"&gt;+&lt;/span&gt; speed &gt; y &lt;span class="jshoperators"&gt;?&lt;/span&gt; y : x &lt;span class="jshoperators"&gt;+&lt;/span&gt; speed;&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// other case, x is greater than y&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;else&lt;/span&gt; &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &gt; y)&lt;br /&gt;    &lt;br /&gt;       &lt;span class="jshcomments"&gt;// well, in this case we remove speed from x&lt;br /&gt;&lt;/span&gt;        x &lt;span class="jshoperators"&gt;=&lt;/span&gt; x &lt;span class="jshoperators"&gt;-&lt;/span&gt; speed &lt; class="jshoperators"&gt;?&lt;/span&gt; y : x &lt;span class="jshoperators"&gt;-&lt;/span&gt; speed;&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; x;&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Simple ? Clear ? ... I like this way as I like ternary operator, it's a must to write compact but efficient code.&lt;br /&gt;However, look at this last function ... don't You see something strange ?&lt;br /&gt;If and else if do exactly same operations ... only &lt;em&gt;plus sign&lt;/em&gt; and &lt;em&gt;greater than&lt;/em&gt; are diferent.&lt;br /&gt;How we could create these similar operations in a single line ?&lt;br /&gt;Using eval, sure !&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; xRun2y(x, y, speed) {&lt;br /&gt;   &lt;span class="jshglobals"&gt;var&lt;/span&gt;    temp &lt;span class="jshoperators"&gt;=&lt;/span&gt; x &lt; class="jshoperators"&gt;?&lt;/span&gt; [&lt;span class="jshstrings"&gt;"+"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"&gt;"&lt;/span&gt;] : [&lt;span class="jshstrings"&gt;"-"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"&lt;"&lt;/span&gt;];&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;&lt;span class="jshmethods"&gt;eval&lt;/span&gt;&lt;/span&gt;(&lt;span class="jshstrings"&gt;"x"&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(temp[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;], &lt;span class="jshstrings"&gt;"speed"&lt;/span&gt;, temp[&lt;span class="jshnumbers"&gt;1&lt;/span&gt;], &lt;span class="jshstrings"&gt;"y?y:x"&lt;/span&gt;, temp[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;], &lt;span class="jshstrings"&gt;"speed"&lt;/span&gt;)); };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Yess !!! ... seems perfect ? ... or seems the evil ? Let me explain that :)&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; xRun2y(x, y, speed) {&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// we need to create dedicated ternary operation&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;var&lt;/span&gt;    temp &lt;span class="jshoperators"&gt;=&lt;/span&gt; x &lt; class="jshoperators"&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       &lt;span class="jshcomments"&gt;// if x is lower than y we need to add speed&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// and verify that x + speed is not greater than y&lt;br /&gt;&lt;/span&gt;        [&lt;span class="jshstrings"&gt;"+"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"&gt;"&lt;/span&gt;] :&lt;br /&gt;&lt;br /&gt;       &lt;span class="jshcomments"&gt;// in other case we need to remove speed from x&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// and check if x is not lower than y&lt;br /&gt;&lt;/span&gt;        [&lt;span class="jshstrings"&gt;"-"&lt;/span&gt;, &lt;span class="jshstrings"&gt;"&lt;"&lt;/span&gt;];&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshcomments"&gt;// if x is lower than y this string is:&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshcomments"&gt;//    x + speed &gt; y ? y : x + speed&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// else if x is greater than y ...&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;//    x - speed &lt; y ? y : x - speed&lt;br /&gt;&lt;/span&gt;    &lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;&lt;span class="jshmethods"&gt;eval&lt;/span&gt;&lt;/span&gt;(&lt;span class="jshstrings"&gt;"x "&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; temp[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;] &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshstrings"&gt;" speed "&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; temp[&lt;span class="jshnumbers"&gt;1&lt;/span&gt;] &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshstrings"&gt;" y ? y : x "&lt;/span&gt; &lt;span class="jshoperators"&gt;+&lt;/span&gt; temp[&lt;span class="jshnumbers"&gt;0&lt;/span&gt;] &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshstrings"&gt;" speed"&lt;/span&gt;); };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;... simple ? cool ? ... &lt;strong&gt;no&lt;/strong&gt;, it's not cool !&lt;br /&gt;Eval in this case isn't absolutely dangerous or a problem while ternary operator is.&lt;br /&gt;The answer is simple, we have reduced code size with a simple and fast function but we do everytime two operations.&lt;br /&gt;These are &lt;em&gt;x + speed&lt;/em&gt; or &lt;em&gt;x - speed&lt;/em&gt; in both cases duplicated.&lt;br /&gt;It's true, a simple addiction shouldn't be a problem for code execution, but if there's a way to use a better function, why we shouldn't use that ?&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; xRun2y(x, y, speed) {&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &lt; y)&lt;br /&gt;       &lt;span class="jshcomments"&gt;// we need minimum value because&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// if x + speed is greater than y&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// we want y&lt;br /&gt;&lt;/span&gt;        x &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;Math&lt;/span&gt;.&lt;span class="jshmethods"&gt;min&lt;/span&gt;(x &lt;span class="jshoperators"&gt;+&lt;/span&gt; speed, y);&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshglobals"&gt;else&lt;/span&gt; &lt;span class="jshglobals"&gt;if&lt;/span&gt;(x &gt; y)&lt;br /&gt;       &lt;span class="jshcomments"&gt;// we need maximum value because&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// if x - speed is lower than y&lt;br /&gt;&lt;/span&gt;        &lt;span class="jshcomments"&gt;// we want y&lt;br /&gt;&lt;/span&gt;        x &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;Math&lt;/span&gt;.&lt;span class="jshmethods"&gt;max&lt;/span&gt;(x &lt;span class="jshoperators"&gt;-&lt;/span&gt; speed, y);&lt;br /&gt;&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; x;&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;Final solution&lt;/strong&gt;&lt;br /&gt;We don't care about case x == y but an operation like this one &lt;em&gt;Math.min(1,1)&lt;/em&gt; returns 1 and not an error than why we couldn't use ternary operator ?&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; xRun2y(x, y, speed) {&lt;br /&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; x &lt; class="jshoperators"&gt;?&lt;/span&gt; &lt;span class="jshglobals"&gt;Math&lt;/span&gt;.&lt;span class="jshmethods"&gt;min&lt;/span&gt;(x &lt;span class="jshoperators"&gt;+&lt;/span&gt; speed, y) : &lt;span class="jshglobals"&gt;Math&lt;/span&gt;.&lt;span class="jshmethods"&gt;max&lt;/span&gt;(x &lt;span class="jshoperators"&gt;-&lt;/span&gt; speed, y);&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Do you think this is the best way to run from a Number to another ? I think so :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3707838969303802155-8250094142918234652?l=andrewpaluba.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewpaluba.blogspot.com/feeds/8250094142918234652/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3707838969303802155&amp;postID=8250094142918234652' title='0 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/8250094142918234652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/8250094142918234652'/><link rel='alternate' type='text/html' href='http://andrewpaluba.blogspot.com/2007/07/running-with-number.html' title='Running with a Number'/><author><name>Andrew</name><uri>http://www.blogger.com/profile/14938351567568650985</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3707838969303802155.post-4419989252450966727</id><published>2007-07-25T16:37:00.000-07:00</published><updated>2007-07-25T16:38:56.350-07:00</updated><title type='text'>bytefx less than 3Kb ? ... NO, less than TWO</title><content type='html'>What an incredible compressor ... i didn't know it but seems really really a good compressor !!!&lt;br /&gt;I'm talking about  memtronic javascript compressor that  allows me to pass from about 2.7 Kb into 1.98 Kb (with comments) !!!&lt;br /&gt;&lt;br /&gt;Sure, it's not a big difference from Dean Edward packer but now I can say that I've created an FX library in exactly 1.889 byte (without comments) ... it's hilarus for me, I didn't know that some compressor was able to reduce size in this way.&lt;br /&gt;&lt;br /&gt;Thank you memtronic !!! Your compressor is fantastic !!!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3707838969303802155-4419989252450966727?l=andrewpaluba.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://andrewpaluba.blogspot.com/feeds/4419989252450966727/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3707838969303802155&amp;postID=4419989252450966727' title='0 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/4419989252450966727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3707838969303802155/posts/default/4419989252450966727'/><link rel='alternate' type='text/html' href='http://andrewpaluba.blogspot.com/2007/07/bytefx-less-than-3kb-no-less-than-two.html' title='bytefx less than 3Kb ? ... NO, less than TWO'/><author><name>Andrew</name><uri>http://www.blogger.com/profile/14938351567568650985</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
