<?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-8283075365682570948</id><updated>2011-04-21T20:19:00.893-07:00</updated><title type='text'>Marc's World</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://marcernandez.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8283075365682570948/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://marcernandez.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Marc</name><uri>http://www.blogger.com/profile/14403682544914879626</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>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8283075365682570948.post-6288627373759913951</id><published>2008-02-23T11:46:00.001-08:00</published><updated>2008-02-23T11:46:33.235-08:00</updated><title type='text'>Could I change a native function ?</title><content type='html'>Just a quick post about how to modify a document (and others generic objects) native methods.&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshcomments"&gt;// basic example&lt;br /&gt;&lt;/span&gt;document.createElement &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;span class="jshglobals"&gt;function&lt;/span&gt;(createElement, Element){&lt;br /&gt;&lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;function&lt;/span&gt;(nodeName){&lt;br /&gt; &lt;span class="jshglobals"&gt;var&lt;/span&gt; element, key;&lt;br /&gt; &lt;span class="jshglobals"&gt;try&lt;/span&gt;{element &lt;span class="jshoperators"&gt;=&lt;/span&gt; createElement(nodeName)}&lt;br /&gt; &lt;span class="jshglobals"&gt;catch&lt;/span&gt;(e){element &lt;span class="jshoperators"&gt;=&lt;/span&gt; createElement.&lt;span class="jshmethods"&gt;call&lt;/span&gt;(document, nodeName)};&lt;br /&gt; &lt;span class="jshglobals"&gt;for&lt;/span&gt;(key &lt;span class="jshglobals"&gt;in&lt;/span&gt; Element)&lt;br /&gt;  element[key] &lt;span class="jshoperators"&gt;=&lt;/span&gt; Element[key];&lt;br /&gt; &lt;span class="jshglobals"&gt;return&lt;/span&gt; element;&lt;br /&gt;}&lt;br /&gt;})(document.createElement, {});&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;What's that ?&lt;/strong&gt;&lt;br /&gt;... a really simple way to extend with your defined object each element created using document.createElement function.&lt;br /&gt;This is an example with useful comments (I hope)&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshcomments"&gt;// redefine document.createElement native function&lt;br /&gt;&lt;/span&gt;&lt;span class="jshcomments"&gt;// using anonymous function&lt;br /&gt;&lt;/span&gt;document.createElement &lt;span class="jshoperators"&gt;=&lt;/span&gt; (&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt;(&lt;br /&gt; &lt;span class="jshcomments"&gt;// original document.createElementFunction&lt;br /&gt;&lt;/span&gt;  createElement,&lt;br /&gt;&lt;br /&gt; &lt;span class="jshcomments"&gt;// object used to extend created elements&lt;br /&gt;&lt;/span&gt;  Element&lt;br /&gt;){&lt;br /&gt;&lt;br /&gt; &lt;span class="jshcomments"&gt;// new document.createElement 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;(&lt;br /&gt;&lt;br /&gt;  &lt;span class="jshcomments"&gt;// type of element (div, p, link, style ... )&lt;br /&gt;&lt;/span&gt;   nodeName&lt;br /&gt; ){&lt;br /&gt;&lt;br /&gt;  &lt;span class="jshcomments"&gt;// element and key to loop over object&lt;br /&gt;&lt;/span&gt;   &lt;span class="jshglobals"&gt;var&lt;/span&gt; element, key;&lt;br /&gt; &lt;br /&gt;  &lt;span class="jshcomments"&gt;// IE like this&lt;br /&gt;&lt;/span&gt;   &lt;span class="jshglobals"&gt;try&lt;/span&gt;{element &lt;span class="jshoperators"&gt;=&lt;/span&gt; createElement(nodeName)}&lt;br /&gt; &lt;br /&gt;  &lt;span class="jshcomments"&gt;// FireFox like that&lt;br /&gt;&lt;/span&gt;   &lt;span class="jshglobals"&gt;catch&lt;/span&gt;(e){element &lt;span class="jshoperators"&gt;=&lt;/span&gt; createElement.&lt;span class="jshmethods"&gt;call&lt;/span&gt;(document, nodeName)};&lt;br /&gt; &lt;br /&gt;  &lt;span class="jshcomments"&gt;// loop over Element&lt;br /&gt;&lt;/span&gt;   &lt;span class="jshglobals"&gt;for&lt;/span&gt;(key &lt;span class="jshglobals"&gt;in&lt;/span&gt; Element)&lt;br /&gt;&lt;br /&gt;  &lt;span class="jshcomments"&gt;// and assign each method to new element&lt;br /&gt;&lt;/span&gt;   element[key] &lt;span class="jshoperators"&gt;=&lt;/span&gt; Element[key];&lt;br /&gt;&lt;br /&gt;  &lt;span class="jshcomments"&gt;// return created element&lt;br /&gt;&lt;/span&gt;   &lt;span class="jshglobals"&gt;return&lt;/span&gt; element;&lt;br /&gt; }&lt;br /&gt;})(&lt;br /&gt;&lt;span class="jshcomments"&gt;// send to anonymous, the original function&lt;br /&gt;&lt;/span&gt; document.createElement,&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// send object used to extend created elements too&lt;br /&gt;&lt;/span&gt; {&lt;br /&gt; &lt;span class="jshcomments"&gt;// read firstChild node&lt;br /&gt;&lt;/span&gt;  read:&lt;span class="jshglobals"&gt;function&lt;/span&gt;(){&lt;br /&gt;  &lt;span class="jshglobals"&gt;return&lt;/span&gt; &lt;span class="jshglobals"&gt;this&lt;/span&gt;.firstChild.nodeValue&lt;br /&gt; },&lt;br /&gt;&lt;br /&gt; &lt;span class="jshcomments"&gt;// write text into node&lt;br /&gt;&lt;/span&gt;  write:&lt;span class="jshglobals"&gt;function&lt;/span&gt;(text){&lt;br /&gt;  &lt;span class="jshglobals"&gt;this&lt;/span&gt;.appendChild(document.createTextNode(text))&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// basic example&lt;br /&gt;&lt;/span&gt;onload &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;var&lt;/span&gt; div &lt;span class="jshoperators"&gt;=&lt;/span&gt; document.createElement(&lt;span class="jshstrings"&gt;"div"&lt;/span&gt;);&lt;br /&gt;document.body.appendChild(div);&lt;br /&gt;div.write(&lt;span class="jshstrings"&gt;"Hello World !!!"&lt;/span&gt;);&lt;br /&gt;alert(div.read()); &lt;span class="jshcomments"&gt;// Hello World !!!&lt;br /&gt;&lt;/span&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That's all, and this method is only a basic native method override example, have fun with JavaScript :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8283075365682570948-6288627373759913951?l=marcernandez.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://marcernandez.blogspot.com/feeds/6288627373759913951/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8283075365682570948&amp;postID=6288627373759913951' title='1 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8283075365682570948/posts/default/6288627373759913951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8283075365682570948/posts/default/6288627373759913951'/><link rel='alternate' type='text/html' href='http://marcernandez.blogspot.com/2008/02/could-i-change-native-function.html' title='Could I change a native function ?'/><author><name>Marc</name><uri>http://www.blogger.com/profile/14403682544914879626</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>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8283075365682570948.post-1189452814941521953</id><published>2008-02-23T11:42:00.000-08:00</published><updated>2008-02-23T11:44:49.498-08:00</updated><title type='text'>Is JSON forgetting something?</title><content type='html'>I played many times with JSON serialized data, creating odd alternatives too like JSTONE or JSOMON.&lt;br /&gt;&lt;br /&gt;Few years ago I created even a PHP compatible serialization but nowadays, with PHP5, json_encode and json_decode are natively integrated in PHP language and, at the same time, even faster than old serialize and unserialize global functions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;The big difference between JSON and PHP serialization&lt;/h3&gt;&lt;br /&gt;Both JSON and PHP Serialized data have the same goal: transport objects and data over http protocol or storage these informations inside a cookie, a database or why not, a file.&lt;br /&gt;However, PHP serialize and unserialize functions do more things than JSON serializzation, adding charset/indexes informations transporting even private or protected parameters.&lt;br /&gt;&lt;br /&gt;With JavaScript 3rd edition We have not latter kind of properties so we shouldn't care about this limit during serializzation.&lt;br /&gt;&lt;br /&gt;This is probably the most important difference between these two different serializzation but there is another one that should be easily implemented in JavaScript too: &lt;em&gt;&lt;a href="http://de3.php.net/manual/en/language.oop.magic-functions.php"&gt;&lt;/a&gt;&lt;/em&gt;magic __sleep and __wakeup methods.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;What do we need&lt;/h3&gt;&lt;br /&gt;The most important information we need to make JSON more magic than ever should be the constructor name. Without this information every kind of instance will lose its special methods/properties reducing them as a common Object.&lt;br /&gt;Usually, this is exactely what we would like to transport, keys and values or, in the case of Array, only its values.&lt;br /&gt;I'm thinking about something like that:&lt;br /&gt;&lt;pre class="code"&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; A(){};&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; B(){};&lt;br /&gt;B.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.__wakeup &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;this&lt;/span&gt;.d &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;String&lt;/span&gt;.&lt;span class="jshmethods"&gt;fromCharCode&lt;/span&gt;(&lt;span class="jshglobals"&gt;this&lt;/span&gt;.c &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshglobals"&gt;this&lt;/span&gt;.a.&lt;span class="jshmethods"&gt;charCodeAt&lt;/span&gt;(&lt;span class="jshnumbers"&gt;0&lt;/span&gt;));&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt; a &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;new&lt;/span&gt; A;&lt;br /&gt;a.a &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshstrings"&gt;"b"&lt;/span&gt;;&lt;br /&gt;a.c &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshstrings"&gt;"d"&lt;/span&gt;;&lt;br /&gt;a.e &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;new&lt;/span&gt; B;&lt;br /&gt;a.e.a &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshstrings"&gt;"b"&lt;/span&gt;;&lt;br /&gt;a.e.c &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshnumbers"&gt;3&lt;/span&gt;;&lt;br /&gt;alert(encode(a));&lt;br /&gt;&lt;br /&gt;&lt;span class="jshcomments"&gt;// $("A",{"a":"b","c":"d","e":$("B",{"a":"b","c":3})})&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;I think that original objects doesn't need their constructor name.&lt;br /&gt;In this way the unique change is for different instances and JSON syntax is quite the same.&lt;br /&gt;&lt;br /&gt;With a string like that we should use eval too in a simple way:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; decode(str){&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; $(&lt;span class="jshproperties"&gt;constructor&lt;/span&gt;, o){&lt;br /&gt; &lt;span class="jshglobals"&gt;var&lt;/span&gt; result &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;(&lt;span class="jshstrings"&gt;"new "&lt;/span&gt;.&lt;span class="jshmethods"&gt;concat&lt;/span&gt;(&lt;span class="jshproperties"&gt;constructor&lt;/span&gt;)), key;&lt;br /&gt; &lt;span class="jshglobals"&gt;for&lt;/span&gt;(key &lt;span class="jshglobals"&gt;in&lt;/span&gt; o){&lt;br /&gt;  &lt;span class="jshglobals"&gt;if&lt;/span&gt;(o.&lt;span class="jshmethods"&gt;hasOwnProperty&lt;/span&gt;(key))&lt;br /&gt;   result[key] &lt;span class="jshoperators"&gt;=&lt;/span&gt; o[key];&lt;br /&gt; };&lt;br /&gt; &lt;span class="jshglobals"&gt;if&lt;/span&gt;(&lt;span class="jshglobals"&gt;typeof&lt;/span&gt; result.__wakeup &lt;span class="jshoperators"&gt;===&lt;/span&gt; &lt;span class="jshstrings"&gt;"function"&lt;/span&gt;)&lt;br /&gt;  result.__wakeup();&lt;br /&gt; &lt;span class="jshglobals"&gt;return&lt;/span&gt; result;&lt;br /&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;(str);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; A(){};&lt;br /&gt;&lt;span class="jshglobals"&gt;function&lt;/span&gt; B(){};&lt;br /&gt;B.&lt;span class="jshproperties"&gt;prototype&lt;/span&gt;.__wakeup &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;this&lt;/span&gt;.d &lt;span class="jshoperators"&gt;=&lt;/span&gt; &lt;span class="jshglobals"&gt;String&lt;/span&gt;.&lt;span class="jshmethods"&gt;fromCharCode&lt;/span&gt;(&lt;span class="jshglobals"&gt;this&lt;/span&gt;.c &lt;span class="jshoperators"&gt;+&lt;/span&gt; &lt;span class="jshglobals"&gt;this&lt;/span&gt;.a.&lt;span class="jshmethods"&gt;charCodeAt&lt;/span&gt;(&lt;span class="jshnumbers"&gt;0&lt;/span&gt;));&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class="jshglobals"&gt;var&lt;/span&gt; o &lt;span class="jshoperators"&gt;=&lt;/span&gt; decode(&lt;span class="jshstrings"&gt;'$("A",{"a":"b","c":"d","e":$("B",{"a":"b","c":3})})'&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;alert([o &lt;span class="jshglobals"&gt;instanceof&lt;/span&gt; A, o.e &lt;span class="jshglobals"&gt;instanceof&lt;/span&gt; B, o.e.d]);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The __wakeup magic method should be used to do something before the object will be assigned, extremely useful in a lot of common situations.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;What about __sleep?&lt;/h3&gt;&lt;br /&gt;The magic __sleep method should be the same used with PHP.&lt;br /&gt;A simple prototype that could do everything but that needs to return properties we want to serialize.&lt;br /&gt;&lt;br /&gt;This should be a JSON parser problem that could check every instance and use, only if it's present, its __sleep method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Improvements&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;constructor name for instances that are not just objects&lt;/li&gt;&lt;br /&gt;&lt;li&gt;better server side parsers to manage instances without loosing their constructor name&lt;/li&gt;&lt;br /&gt;&lt;li&gt;a global name for dollar function, if some library use them as constructor too to avoid problems during instance creation (or a simple global-scope evaluation for each new "constructor" assignment)&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is just a personal brainstorming/proposal and I would like to know what do you think about that.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8283075365682570948-1189452814941521953?l=marcernandez.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://marcernandez.blogspot.com/feeds/1189452814941521953/comments/default' title='Objavi komentare'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8283075365682570948&amp;postID=1189452814941521953' title='0 komentara'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8283075365682570948/posts/default/1189452814941521953'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8283075365682570948/posts/default/1189452814941521953'/><link rel='alternate' type='text/html' href='http://marcernandez.blogspot.com/2008/02/is-json-forgetting-something.html' title='Is JSON forgetting something?'/><author><name>Marc</name><uri>http://www.blogger.com/profile/14403682544914879626</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>
