<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dayton Nolan</title>
	<atom:link href="http://www.daytonnolan.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daytonnolan.com</link>
	<description></description>
	<lastBuildDate>Wed, 14 Sep 2011 02:07:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Strict Type Checking in JavaScript</title>
		<link>http://www.daytonnolan.com/strict-type-checking-in-javascript-2/</link>
		<comments>http://www.daytonnolan.com/strict-type-checking-in-javascript-2/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 00:17:48 +0000</pubDate>
		<dc:creator>daytonn</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.daytonnolan.com/?p=11</guid>
		<description><![CDATA[JavaScript, as you may know, does not have very strict type checking. The typeof operator can pretty much only tell you the difference between Strings, Numbers, and Objects. This just isn&#8217;t going to cut it if you need to make decisions based on specific Object types, including custom Objects. The good news is there is [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript, as you may know, does not have very strict type checking. The <em>typeof</em> operator can pretty much only tell you the difference between Strings, Numbers, and Objects. This just isn&#8217;t going to cut it if you need to make decisions based on specific Object types, including custom Objects. The good news is there is a way to get at this information in JavaScript pretty easily:</p>
<pre name="code" class="brush: js;">
  var test_string = 'Hello',
      test_array = ['one', 'two', 'three'],
      test_number = 42;
      test_object = { key: 'value' };
      test_function = function() {};

  test_string.constructor == String;// true
  test_array.constructor == Array;// true
  test_number.constructor == Number;// true
  test_object.constructor == Object;// true
  test_function.constructor == Function;// true

  test_string.constructor == Object;// false
  test_array.constructor == Object;// false
  test_number.constructor == Object;// false
  test_object.constructor == Function;// false
  test_function.constructor == Object;// false
</pre>
<p>The only drawback is that it&#8217;s a bit cumbersome, and a little confusing. I like to wrap this in a bit of sugar.</p>
<pre name="code" class="brush: js;">
  function is_typeof(type, suspect) {
    if (type === undefined) {
      throw new Error('is_typeof(type, suspect): type is undefined.');
    }

    if (suspect === undefined) {
      throw new Error('is_typeof(type, suspect): suspect is undefined.');
    }

    return suspect.constructor == type;
  }
</pre>
<p><em><br />
  Note: I use snake case because I like it, feel free to judge me and change it to something you&#8217;d prefer.<br />
</em></p>
<p>I go with is_typeof because it&#8217;s a little more definitive and it bears resemblance to the well-known typeof keyword so it indicates what it does. I&#8217;m open to better names for this one but I think it&#8217;s short sweet and to the point.</p>
<p>This may look a bit weird, but it makes for a pretty clear syntax. Let&#8217;s take a look at this in action:</p>
<pre name="code" class="brush: js;">
  function Unicorn(arg) {

    if (is_typeof(String, arg)) {
      this.name = arg;
    }

    if (is_typeof(Object, arg)) {
      var obj = arg;
      for (var prop in obj) {
        if(obj.hasOwnProperty(prop)) {
          this[prop] = obj[prop];
        }
      }
    }

    return Unicorn;
  };
</pre>
<p>Here&#8217;s a simple example of a basic custom object, with the ability to accept either a string or an object in the constructor. This gives us a sort-of dual API to create Unicorns. How sweet is that. Seriously, you can make some pretty useful functions with this pattern. Although I should point out that you shouldn&#8217;t do this just because you can, this technique is an investment in complexity and too much of it is unhealthy. If you&#8217;re testing, which you should be, this adds a significant amount of surface area to test. It works great for constructors and cases where you want the same basic action to be run with more or less data.</p>
<p>This is only one simple technique using the is_typeof method. Another use is hardening up your code by making strict type requirements, among others. It&#8217;s a good trick to have in your bag and it&#8217;s syntax makes it pretty easy to figure out at a glance. It really helps you write better code.</p>
<p>If you want to take this to the next level you can curry this function to make shorthand methods for any type you like:</p>
<pre name="code" class="brush: js;">
  function is_string(suspect) {
    return is_typeof(String, suspect);
  }
</pre>
<p>This makes code like the following pretty short and sweet:</p>
<pre name="code" class="brush: js;">
  var greeting = 'Hello World!';

  if (is_string(greeting)) {
    alert(greeting);
  }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.daytonnolan.com/strict-type-checking-in-javascript-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

