tag:blogger.com,1999:blog-34454975.post5964308147377122044..comments2023-06-28T16:58:41.189+02:00Comments on Web Reflection: 94 bytes to quickly get a var typeAndrea Giammarchihttp://www.blogger.com/profile/16277820774810688474noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-34454975.post-8441305484426110232009-12-23T23:26:06.292+01:002009-12-23T23:26:06.292+01:00Hi again. Last few thoughts.
Why not use Michael&...Hi again. Last few thoughts.<br /><br />Why not use Michael's String method but apply it to u. Wouldn't this now be optimized as it doesn't change?<br /><br />(function(s,u){return function(o){return o===u?String(u):o?s.call(o).slice(8,-1):"null"}})({}.toString);<br /><br />(I realize the point was for it to be short for a tweet, but this is very useful code.)<br /><br />Also (can you tell I just read the comments? :o), if we don't care about null/undefined except for testing why not return false for those? Now we can use:<br /> if (!!type(o)) ...<br />and<br /> if (!type(o)) ...<br />This seems to work really well.<br /><br />I think this is all just brilliant. I'm going to replace the call(o).slice with a call to a private function within this one for more processing and revisit my work of several weeks ago.<br /><br />Thanks very much.Paul (PVHL)https://www.blogger.com/profile/04876266705027266653noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-8449195094134458692009-12-23T22:40:18.176+01:002009-12-23T22:40:18.176+01:00I was playing with type routines recently because ...I was playing with type routines recently because I was building a tool to dynamically build code from objects whose methods were scattered all over the code I was trying to understand. All the examples I found were enormous.<br /><br />This code initially caused shock and awe, showing just how much I have to learn. The "null" value gives different results in Gecko, IE6, and Chrome (window, object, builtins (!)) so my version is:<br /><br />var type=(function(s,u){return function(o){return o===u?""+o:o?s.call(o).slice(8,-1):"null"}})({}.toString);<br /><br />I changed the comparison because I'm more concerned with speed. Would this make a difference as conversion might be avoided? (I intend to test but thought you'd probably know).<br /><br />This works well with DOM objects too, except in IE, which returns "Object".<br /><br />Brilliant work Andrea. I'm learning lots from you. Have a great time in Italy (and Germany).Paul (PVHL)https://www.blogger.com/profile/04876266705027266653noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-11976968493872563892009-09-02T08:49:50.919+02:002009-09-02T08:49:50.919+02:00is a function call (performances) over a weird cas...is a function call (performances) over a weird case (test null or undefined) for a check that in any case will be:<br /><br /><i>if(type(o) === "undefined"){<br /> ... that was another string ...<br />}</i><br /><br />;)Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-27030414647290043142009-09-02T01:34:30.889+02:002009-09-02T01:34:30.889+02:00Mostly happy ;-p
I used String(o) instead of &quo...Mostly happy ;-p<br /><br />I used <b>String(o)</b> instead of <b>""+o</b> because it creates one string instead of the two (or three?) created by: <b>""</b> and <b>""+o.toString()</b>.<br /><br />Figured that would match your code optimization goal ;-){Michael : iSkitz}https://www.blogger.com/profile/07632896592054092882noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-79552451577287162362009-09-02T00:18:52.340+02:002009-09-02T00:18:52.340+02:00OK Michael, I have updated the code with the short...OK Michael, I have updated the code with the shortest version considering null or undefined cannot have a predefined toString with a result different from ""+o, which I prefer over String(o).<br />I have added var for kangax happiness, and I called it just type.<br />Everybody happy now? :D<br />The initial aim was still to retrieve the internal [[Class]] though ... nevermindAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-57382533561855832642009-09-01T22:42:50.821+02:002009-09-01T22:42:50.821+02:00Andrea,
Nice. Works in most cases except if o is n...Andrea,<br />Nice. Works in most cases except if o is null or undefined. Here's an updated version that works with null and undefined too:<br /><br /><b>getType</b>=(function(s<b>,u</b>){return function(o){return <b>o==u?String(o):</b>s.call(o).slice(8,-1)}})({}.toString);<br /><br />This was fun, but isn't function decompilation unreliable?{Michael : iSkitz}https://www.blogger.com/profile/07632896592054092882noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-62706715579232660162009-09-01T17:02:44.368+02:002009-09-01T17:02:44.368+02:00@Anonymous, in ECMAScript 3 there are no classes b...@Anonymous, in ECMAScript 3 there are no classes but there is an internal [[Class]] which is associated in core.<br /><br />This function aim is to easily know what kind of internal class the passed object has.<br /><br />getClassName([]) === "Array"<br />for example<br /><br />@kangax<br />it was a #tweetcode, but being suitable for a global scope I do not think that missed var is that problem. At the same time you can attach this function into an object.<br />$.getClassName = (...)<br />or just put var in front of it if you want to use it inside a closure.<br />I can't spot side-effects at all for a global scope, do you?Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-3685692437863604802009-09-01T15:26:02.575+02:002009-09-01T15:26:02.575+02:00undeclared assignment? not a good idea...undeclared assignment? not a good idea...kangaxhttps://www.blogger.com/profile/16684075828108388379noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-18844720209013042252009-09-01T14:53:44.954+02:002009-09-01T14:53:44.954+02:00Sorry, but how this is supposed to work?
eg:
func...Sorry, but how this is supposed to work?<br /><br />eg:<br />function Pippo(){};<br />getClassName(new Pippo); // Object ??Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-34454975.post-50677116810117204192009-09-01T11:52:42.608+02:002009-09-01T11:52:42.608+02:00np :)np :)Andrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-34868276124696006402009-09-01T11:49:31.920+02:002009-09-01T11:49:31.920+02:00Sure!
Thank youSure!<br />Thank youOopshttps://www.blogger.com/profile/17600901862664625889noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-50942943368217926212009-09-01T11:47:27.494+02:002009-09-01T11:47:27.494+02:00the first one does not wrap the original Object.pr...the first one does not wrap the original Object.prototype.toString for the entire session, while the second one will fail with null, undefined, and host objectsAndrea Giammarchihttps://www.blogger.com/profile/16277820774810688474noreply@blogger.comtag:blogger.com,1999:blog-34454975.post-30075395771474836302009-09-01T11:45:50.334+02:002009-09-01T11:45:50.334+02:00Sorry Andrea, I'm not a JSNinja like you are :...Sorry Andrea, I'm not a JSNinja like you are :P<br /><br />Do you think is wrong rewrite your code in this way:<br /><br />getClassName=function(o){<br /> return {}.toString.call(o).slice(8,-1)<br />};<br /><br />Or just:<br /><br />getClassName=function(o){<br /> return o.toString().slice(8,-1)<br />};Oopshttps://www.blogger.com/profile/17600901862664625889noreply@blogger.com