{"id":1056,"date":"2012-11-10T15:12:16","date_gmt":"2012-11-10T15:12:16","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=1056"},"modified":"2012-11-22T01:28:31","modified_gmt":"2012-11-22T01:28:31","slug":"javascript-did-you-know","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/javascript-did-you-know\/","title":{"rendered":"JavaScript &#8211; Did you know that?"},"content":{"rendered":"<h3>Pain in the arse int, parseInt()<\/h3>\n<p>How often do you use <kbd>parseInt()<\/kbd>? Often enough to have run into the unexpected &quot;starts with 0&quot; problem? Have you ever tried: <kbd>parseInt(&quot;08&quot;)<\/kbd>, only to get a return of 0? Let&#39;s find out why.<\/p>\n<p>To start, <kbd>parseInt() <\/kbd>accepts a base as the second argument.&nbsp; You may sometimes see plugins use parseInt(<em>number<\/em>, 10), wonder why added this mysterious 10 as a second argument.&nbsp; Or, perhaps you knew about the base parameter, but wonder why the explicitly specify it.<\/p>\n<p>The <kbd>parseInt(&quot;08&quot;)<\/kbd> issue identified above is the reason. Computers love base 2, but base 8 (octal) and base 16 (hexadecimal) are also happily received as they are powers of 2. We use the hex base commonly for RGB color and other values, so languages developed a syntax to easily specify the numeric base. In JavaScript, this notation is as follows:<\/p>\n<p>0x<em>number =<\/em> Hexadecimal<br \/>\n\t0<em>number<\/em> = Octal<\/p>\n<p>You can actually use this is a numeric literal:<\/p>\n<p><code>x = 0xF;<br \/>\n\t<span style=\"color:#00f;\">15<\/span><\/code><br \/>\n\t<code><br \/>\n\tx = 033;<br \/>\n\t<span style=\"color:#00f;\">27<\/span><\/code><\/p>\n<p>\tThe octal notation comes as a surprise, especially if you&#39;ve even been unlucky enough to accidentally precede a numeric literal with a zero. Why JavaScript couldn&#39;t use a notation like &quot;0w33&quot;, I will never know.<\/p>\n<p>Everything I&#39;ve said to this point typically does not present an issue in everyday JavaScript. However, when you introduce user input and other string values, our old friend <kbd>parseInt()<\/kbd> comes to the rescue, but not without terms. A user will never type 0xFF in place of the base 10 number 255. But, if a user is asked for their birthday, and they use &quot;08&quot; to represent the month of August, we now have an octal number! What&#39;s worse, an octal number that is invalid (there&#39;s no 8 in the octal base).<\/p>\n<p><code>parseInt(&quot;08&quot;)<br \/>\n\t<span style=\"color:#00f;\">0<\/span><br \/>\n\t<\/code><\/p>\n<p>And, that concludes the very long winded rationale for always adding the number base parameter 10, when you are converting strings to integers.<\/p>\n<p><code>parseInt(&quot;08&quot;, 10)<br \/>\n\t<\/code><\/p>\n<p>&nbsp;<\/p>\n<h3>Time out, I see something <em>eval<\/em>() happening here<\/h3>\n<p>I&#39;ll be brief with this one, as I assume most people know about this. However, I still see quite a bit of JavaScript floating around that suggests using &quot;quoted&quot; code within the <kbd>setTimeout()<\/kbd> function:<\/p>\n<p><code>setTimeout(&#39;do = &quot;this&quot;&#39;, 3000);<\/code><\/p>\n<p>I can&#39;t say for sure, but everything I know about JavaScript tells me that <kbd>setTimeout()<\/kbd> will be performing an eval() on the quoted code, which we know is inefficient.<\/p>\n<p><kbd>setTimeout()<\/kbd> can, and should be given a function as it&#39;s first argument:<\/p>\n<p><code>setTimeout(function() { do = &#39;this&#39;; }, 3000);<\/code><\/p>\n<p>&nbsp;<\/p>\n<h3>Can&#39;t touch <em>this<\/em><\/h3>\n<p>The <kbd>this<\/kbd> keyword provides a reference to the current object, it provides context. But, we lose that context when we make a call to another function. How often do you find yourself doing this:<\/p>\n<p><code>(function() {<br \/>\n\t&nbsp; var _this = this;<br \/>\n\t&nbsp; doSomething() {<br \/>\n\t&nbsp;&nbsp;&nbsp; console.log(this);&nbsp;&nbsp; \/\/ this is no longer meaningful - it doesn&#39;t refer to the original object <br \/>\n\t&nbsp;&nbsp;&nbsp; _this.value = &#39;I had to copy this to a local variable so I still have a reference to it&#39;;<br \/>\n\t&nbsp; }<br \/>\n\t&nbsp; <br \/>\n\t&nbsp; doSomething();<\/code><br \/>\n\t<code>})();<br \/>\n\t<\/code><\/p>\n<p>In this case, once we stepped inside the inner function, doSomething(), we lost all context that <kbd>this <\/kbd>provided. But, we were able to maintain that context, by storing it in a local variable called <kbd>_this<\/kbd>.<\/p>\n<p>I don&#39;t know if I&#39;d call this a bad practice, I see it a lot, it works, but perhaps doesn&#39;t do a good job of explaining what <kbd>_this<\/kbd> is. Perhaps the variable should be named something more appropriate, like:<\/p>\n<p><code>var myParent = this;&nbsp;&nbsp; \/\/or<br \/>\n\tvar myCaller = this;<br \/>\n\t<\/code><\/p>\n<p>But, there&#39;s a way we can avoid this temporary reference to the <kbd>this<\/kbd> context. We can do this by using the built-in <kbd>call()<\/kbd> method.<\/p>\n<p><code>(function() {<br \/>\n\t&nbsp; doSomething() {<br \/>\n\t&nbsp;&nbsp;&nbsp; console.log(this);&nbsp;&nbsp; \/\/ this is now usable, it references the calling object<br \/>\n\t&nbsp;&nbsp;&nbsp; this.value = &#39;I have no need for a temporary reference to my caller&#39;s this context&#39;;<br \/>\n\t&nbsp; }<br \/>\n\t&nbsp; <br \/>\n\t&nbsp; doSomething.call(this);<br \/>\n\t})();<\/code><\/p>\n<p><kbd>call()<\/kbd> allows us to &quot;pass in&quot; the context that the function should have. So, we&#39;re essentially saying, &quot;when I call you, stay in the same context, you don&#39;t have a context, suck it!&quot;<\/p>\n<p>jQuery tells us this is okay by providing a <em>context<\/em> parameter to their <a href=\"http:\/\/api.jquery.com\/jQuery.ajax\/\" target=\"_blank\">ajax <\/a>method. This parameter provides the callback methods with the proper context. Usually, we just pass in <kbd>this<\/kbd>.<\/p>\n<p>&nbsp;<\/p>\n<h3>Semicolons are super important<\/h3>\n<p>Okay, so nobody likes semicolons. The word semicolon doesn&#39;t even really make sense, how can something that&#39;s &quot;semi&quot; be larger than the whole thing (colon)? But, as a symbol, they make a lot of sense to the JavaScript interpreter. They say, &quot;Hey, this statement has reached it&#39;s end.&quot;<\/p>\n<p>The problem is, the interpreter is not strict on their usage, most&nbsp; of the time you can get away without using a semicolon at the end of a statement. This flexibility actually leads us to believe that they&#39;re optional, and, in most cases, they are.<\/p>\n<p>But, what happens when you create an object, forget to close it with a semicolon, then try to run an anonymous function? Surprise! We get an error. Try the following code to see for yourself:<\/p>\n<p><code>x = { x: &#39;y&#39;}<br \/>\n\t(function() {<br \/>\n\t&nbsp; var y = x;<br \/>\n\t})();<br \/>\n\t<span style=\"color:#f00;\">Uncaught TypeError: object is not a function<\/span><br \/>\n\t<\/code><\/p>\n<p>Drop a semicolon at the end of the first statement, and you can continue on your way.<\/p>\n<p>\t&nbsp;<\/p>\n<h3>Keep your window clean<\/h3>\n<p>If I were to describe JavaScript to a C++ or Java developer in one sentence, I&#39;d say, &quot;It&#39;s powerful, flexible and fun, but it&#39;s a lot like learning the English language.&quot; The English language has some quirks, such as:<\/p>\n<p>Plural of goose = geese<\/p>\n<p>Now, you&#39;re not going to cause any damage if you say, &quot;I fed the gooses this morning&quot;, but you can really make a mess when you misuse the JavaScript <em>window<\/em> object. The <em>window<\/em> object is a product of the browser JavaScript engine, it provides us with methods and properties that allow us to interact directly with the web browser. But, it often creates a confusion for the following reasons:<\/p>\n<ul>\n<li>It lacks cohesion among it&#39;s properties and methods:\n<ul>\n<li><kbd>window.length()<\/kbd> returns the number of frames that the window contains. Huh? The window object contains a frames object. Why would window.length() be assigned the number of elements in that frames object.<br \/>\n\t\t\t\t&nbsp;<\/li>\n<li><kbd>window.open()<\/kbd> creates a new window. It&#39;s confusing to use one instance of an object to create another instance of the same object.<br \/>\n\t\t\t\t&nbsp;<\/li>\n<li>what happens when we decide provide access to other browser tabs that have loaded the same domain?<kbd> window.tabs()<\/kbd> doesn&#39;t really make much sense, yet window is the &quot;top-most&quot; object.<br \/>\n\t\t\t\t&nbsp;<\/li>\n<\/ul>\n<\/li>\n<li>That lack of cohesion is made worse once you add &quot;global&quot; objects in your own code\n<ul>\n<li>When you define a variable outside of a local scope, it becomes a global variable, accessible by any and all other objects, across all scripts loaded in that page request.<br \/>\n\t\t\t\t&nbsp;<\/li>\n<li>The window object becomes the new home for these global variables:<br \/>\n\t\t\t\t<code><br \/>\n\t\t\t\ta = 1;<br \/>\n\t\t\t\twindow.a<br \/>\n\t\t\t\t<span style=\"color:#00f;\">1<\/span><\/code><br \/>\n\t\t\t\t<code><br \/>\n\t\t\t\ta === window.a<br \/>\n\t\t\t\t<span style=\"color:#00f;\">true<\/p>\n<p>\t\t\t\t<\/span><\/code><\/li>\n<li>You&#39;re a smart programmer, so you use the local scope provided by the <em>var<\/em> keyword:\n<p>\t\t\t\t<code>function doThis() {<br \/>\n\t\t\t\t&nbsp; var b = 1;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t\twindow.b<br \/>\n\t\t\t\t<span style=\"color:#00f;\">undefined<\/span><\/code><\/p>\n<p>\t\t\t\tbut,<\/p>\n<p>\t\t\t\t<code>window.doThis === doThis<br \/>\n\t\t\t\t<span style=\"color:#00f;\">true<\/span><\/code><\/p>\n<p>\t\t\t\tSo, the <em>window<\/em> object is a little greedy, it not only wants your global variables, but your global functions. It seems like a catch-22. How can I keep the global space clean by using, then disposing a local variable, if doing so requires that I create a global function?<\/p>\n<p>\t\t\t\tThe JavaScript dudes thought about this though, and decided to allow a function to be created and called without ever assigning it a name and position in the global space (window object).<\/p>\n<p>\t\t\t\tThey call this the <strong>anonymous function<\/strong>:<\/p>\n<p>\t\t\t\t<code>(function() {&nbsp;&nbsp;&nbsp; <br \/>\n\t\t\t\t&nbsp; var a = 1;<br \/>\n\t\t\t\t})();&nbsp;&nbsp;&nbsp; <br \/>\n\t\t\t\twindow.a;<br \/>\n\t\t\t\t<span style=\"color:#00f;\">undefined<\/span><\/code><\/p>\n<p>\t\t\t\tEssentially, what we did was create a function without a name, but wrapped the entire function definition in parenthesis. We did this, so we can call it with the ( ) function-call syntax we&#39;re familiar with, directly after we define it.<\/p>\n<p>\t\t\t\tSo, we not only kept a variable from being added to the global space, but also prevent a function name from taking a seat at the global table.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Pain in the arse int, parseInt() How often do you use parseInt()? Often enough to have run into the unexpected &quot;starts with 0&quot; problem? Have&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1099,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[218],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1056"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":12,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":1101,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1056\/revisions\/1101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/1099"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}