<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Javascript Type Gotchas</title>
	<atom:link href="http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 15:00:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Peter</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-10433</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Mon, 04 Apr 2011 22:56:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-10433</guid>
		<description>I&#039;ve talked to some fellow Scala programmers about whether you could do this using Scala. At the simplest you can use Scala objects which represent Javascript code and can be converted to Javascript (&lt;a href=&quot;http://main.scala-tools.org/mvnsites/liftweb-2.2/framework/scaladocs/net/liftweb/http/js/JE$object.html&quot; rel=&quot;nofollow&quot;&gt;such as Lift&#039;s&lt;/a&gt;) but even cooler would be a DSL (and probably compiler plugin) which would let you program Javascript but actually have it be correct, statically-typed Scala behind the scenes. People tell me it&#039;s possible...</description>
		<content:encoded><![CDATA[<p>I&#8217;ve talked to some fellow Scala programmers about whether you could do this using Scala. At the simplest you can use Scala objects which represent Javascript code and can be converted to Javascript (<a href="http://main.scala-tools.org/mvnsites/liftweb-2.2/framework/scaladocs/net/liftweb/http/js/JE$object.html" rel="nofollow">such as Lift&#8217;s</a>) but even cooler would be a DSL (and probably compiler plugin) which would let you program Javascript but actually have it be correct, statically-typed Scala behind the scenes. People tell me it&#8217;s possible&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-10432</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Mon, 04 Apr 2011 22:49:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-10432</guid>
		<description>It would be interesting to implement a static typechecker for javascript. I am working at a project for which we are developing a framework inspired by reactive programming in javascript. Because the maintainability suffered under the amount of callbacks. We separated the output to the outside world (the calling of a function, which accepts a callback) from the input from the outside world (the result, which comes from the callback). And the processing functions between the inputs and the outputs should be pure. As consequence of this, concurrency arises naturally. 

Types become more and more important in the framework.

Static typechecking would be very handy. But dynamic type checking with good error messages is a big step forward.</description>
		<content:encoded><![CDATA[<p>It would be interesting to implement a static typechecker for javascript. I am working at a project for which we are developing a framework inspired by reactive programming in javascript. Because the maintainability suffered under the amount of callbacks. We separated the output to the outside world (the calling of a function, which accepts a callback) from the input from the outside world (the result, which comes from the callback). And the processing functions between the inputs and the outputs should be pure. As consequence of this, concurrency arises naturally. </p>
<p>Types become more and more important in the framework.</p>
<p>Static typechecking would be very handy. But dynamic type checking with good error messages is a big step forward.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-10099</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Sun, 13 Feb 2011 10:45:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-10099</guid>
		<description>Thanks, Peter. You&#039;re right that typed.js does do recursive type-checking. However, it was a quick hack to make arrays with subtypes work. I&#039;ve been thinking about how to revisit it to make any type of collection work, whether based on generic objects or otherwise organized.

For example, I&#039;d like to figure out whether multiple type parameters can be supported in an elegant way. I already use an array for type parameters but I haven&#039;t yet figured out how to check each type parameter without requiring the object or its type object to hand its parts off to the checker. I&#039;d like to avoid messing with Array, Object, and other standard parts of Javascript.</description>
		<content:encoded><![CDATA[<p>Thanks, Peter. You&#8217;re right that typed.js does do recursive type-checking. However, it was a quick hack to make arrays with subtypes work. I&#8217;ve been thinking about how to revisit it to make any type of collection work, whether based on generic objects or otherwise organized.</p>
<p>For example, I&#8217;d like to figure out whether multiple type parameters can be supported in an elegant way. I already use an array for type parameters but I haven&#8217;t yet figured out how to check each type parameter without requiring the object or its type object to hand its parts off to the checker. I&#8217;d like to avoid messing with Array, Object, and other standard parts of Javascript.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-10096</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Sat, 12 Feb 2011 20:08:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-10096</guid>
		<description>Cool, they look really the same, but I like your named parameters more. You also doing recursive type checking. (From a quick glance of your code). Yours is much more advanced. Catching errors were a problem. But you seem to have found a elegant solution. 

I wrote mine to make working with higher order functions easier. It was a hack to make the typesystem of javascript stronger.  I like to write my code in a functional style. 

Good work and thank for sharing. If I used it in a project and extend it, I will leave you a message. Cheers.</description>
		<content:encoded><![CDATA[<p>Cool, they look really the same, but I like your named parameters more. You also doing recursive type checking. (From a quick glance of your code). Yours is much more advanced. Catching errors were a problem. But you seem to have found a elegant solution. </p>
<p>I wrote mine to make working with higher order functions easier. It was a hack to make the typesystem of javascript stronger.  I like to write my code in a functional style. </p>
<p>Good work and thank for sharing. If I used it in a project and extend it, I will leave you a message. Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-9912</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Wed, 12 Jan 2011 12:27:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-9912</guid>
		<description>Thanks, Peter, I hadn&#039;t see that library. However, I think my (incomplete) &lt;a href=&quot;https://github.com/pr1001/typed.js&quot; rel=&quot;nofollow&quot;&gt;typed.js&lt;/a&gt; library is a little nicer:

&lt;pre lang=&quot;javascript&quot;&gt;
var test = T.typedFunction(
  {x: T.StringType, y: T.StringType, z: T.StringType},
  T.ArrayStringType,
  function(x, y, z) {
    return [x, y, z];
  }
);
test(&#039;x&#039;, &#039;y&#039;, &#039;z&#039;) // -&gt; [&#039;x&#039;, &#039;y&#039;, &#039;z&#039;]
test(1, &#039;x&#039;, &#039;z&#039;) // -&gt; Error: Parameter x does not satisfy type condition of type String with condition function condition(obj) { return T.is(obj, this.type); }
&lt;/pre&gt;

Of course they&#039;re quite similar, so your mileage may vary. I guess mine is a bit more verbose, but things like named parameters mean that my error messages are quite nice.

You&#039;ll notice that the error talks about type conditions because typed functions in my system can actually specify not just the simple type but rather the conditions based upon it, such as all sub-types of String, such as if you wrote your own RichString extension to String. The README has more about type conditions and implicit conversions (an implicit conversion is being done from the StringType types I provided to type conditions which test for simple equality with the type).</description>
		<content:encoded><![CDATA[<p>Thanks, Peter, I hadn&#8217;t see that library. However, I think my (incomplete) <a href="https://github.com/pr1001/typed.js" rel="nofollow">typed.js</a> library is a little nicer:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> test <span style="color: #339933;">=</span> T.<span style="color: #660066;">typedFunction</span><span style="color: #009900;">&#40;</span>
  <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span> T.<span style="color: #660066;">StringType</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> T.<span style="color: #660066;">StringType</span><span style="color: #339933;">,</span> z<span style="color: #339933;">:</span> T.<span style="color: #660066;">StringType</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  T.<span style="color: #660066;">ArrayStringType</span><span style="color: #339933;">,</span>
  <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> z<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> z<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'x'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'y'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'z'</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// -&gt; ['x', 'y', 'z']</span>
test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'x'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'z'</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// -&gt; Error: Parameter x does not satisfy type condition of type String with condition function condition(obj) { return T.is(obj, this.type); }</span></pre></div></div>

<p>Of course they&#8217;re quite similar, so your mileage may vary. I guess mine is a bit more verbose, but things like named parameters mean that my error messages are quite nice.</p>
<p>You&#8217;ll notice that the error talks about type conditions because typed functions in my system can actually specify not just the simple type but rather the conditions based upon it, such as all sub-types of String, such as if you wrote your own RichString extension to String. The README has more about type conditions and implicit conversions (an implicit conversion is being done from the StringType types I provided to type conditions which test for simple equality with the type).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter van der Heijden</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-9911</link>
		<dc:creator>Peter van der Heijden</dc:creator>
		<pubDate>Wed, 12 Jan 2011 12:06:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-9911</guid>
		<description>This seems to be relevant, it implements a strong type system in javascript:

http://sourceforge.net/projects/jtypesystem/files/

From the documentation:

test = FT(TString, TString, TString, TArray,
function(x,y,z){
return new Array(x,y,z);
});
/* Ok */
test(&#039;x&#039;,&#039;y&#039;,&#039;z&#039;);
/* Not ok */
test(1,&#039;x&#039;,&#039;z&#039;);</description>
		<content:encoded><![CDATA[<p>This seems to be relevant, it implements a strong type system in javascript:</p>
<p><a href="http://sourceforge.net/projects/jtypesystem/files/" rel="nofollow">http://sourceforge.net/projects/jtypesystem/files/</a></p>
<p>From the documentation:</p>
<p>test = FT(TString, TString, TString, TArray,<br />
function(x,y,z){<br />
return new Array(x,y,z);<br />
});<br />
/* Ok */<br />
test(&#8216;x&#8217;,'y&#8217;,'z&#8217;);<br />
/* Not ok */<br />
test(1,&#8217;x',&#8217;z');</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-6236</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Fri, 28 May 2010 23:04:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-6236</guid>
		<description>Yep, you&#039;ve basically got it Collin.

However, there are no such things as classes in Javascript. Rather, &lt;code&gt;instanceof&lt;/code&gt; tells you whether the left-hand side object is in the right-hand side object&#039;s prototype chain. This is why &lt;code&gt;(new String) instanceof String == true&lt;/code&gt; and &lt;code&gt;(new String) instanceof Object == true&lt;/code&gt;: both &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Object&lt;/code&gt; are in &lt;code&gt;new String&lt;/code&gt;&#039;s prototype chain. Granted, the same would be true if &lt;code&gt;String&lt;/code&gt; were a subclass of &lt;code&gt;Object&lt;/code&gt; in a class-based inheritance system, but remember that Javascript uses a prototype-based inheritance system and there can and often are subtle differences.

When you use the &lt;code&gt;new&lt;/code&gt; operator on a function (say &lt;code&gt;String&lt;/code&gt;) in Javascript, it is essentially the same as &lt;code&gt;String.call({})&lt;/code&gt;. That is, the &lt;code&gt;String&lt;/code&gt; function is being called with an empty object as its &lt;code&gt;this&lt;/code&gt; value. This &lt;code&gt;this&lt;/code&gt; (excuse the pun) functionality means that you can have something akin to &lt;a href=&quot;http://en.wikipedia.org/wiki/Mixin&quot; rel=&quot;nofollow&quot;&gt;mixins&lt;/a&gt;. &lt;code&gt;String.call(Number.call({}))&lt;/code&gt; should give you an object with both string and number methods. I haven&#039;t tested this, so I can&#039;t promise it works.

So, returning to your conclusions from the examples, your explanations are spot on except that &lt;code&gt;null&lt;/code&gt; really isn&#039;t an object. I believe it&#039;s a mistake in Javascript to return &lt;code&gt;&quot;object&quot;&lt;/code&gt; from &lt;code&gt;typeof null&lt;/code&gt;. Instead, I believe it is a primitive, not an object, and should return &lt;code&gt;null&lt;/code&gt;.

The solution I was hinting at, and still need to blog about, is &lt;a href=&quot;http://github.com/pr1001/typed.js&quot; rel=&quot;nofollow&quot;&gt;typed.js&lt;/a&gt;, a better, &#039;replacement&#039; type system for Javascript. It will give you consistent, rational type information. For instance, &lt;code&gt;null&lt;/code&gt; is always a &lt;code&gt;NullType&lt;/code&gt; in my type system.</description>
		<content:encoded><![CDATA[<p>Yep, you&#8217;ve basically got it Collin.</p>
<p>However, there are no such things as classes in Javascript. Rather, <code>instanceof</code> tells you whether the left-hand side object is in the right-hand side object&#8217;s prototype chain. This is why <code>(new String) instanceof String == true</code> and <code>(new String) instanceof Object == true</code>: both <code>String</code> and <code>Object</code> are in <code>new String</code>&#8216;s prototype chain. Granted, the same would be true if <code>String</code> were a subclass of <code>Object</code> in a class-based inheritance system, but remember that Javascript uses a prototype-based inheritance system and there can and often are subtle differences.</p>
<p>When you use the <code>new</code> operator on a function (say <code>String</code>) in Javascript, it is essentially the same as <code>String.call({})</code>. That is, the <code>String</code> function is being called with an empty object as its <code>this</code> value. This <code>this</code> (excuse the pun) functionality means that you can have something akin to <a href="http://en.wikipedia.org/wiki/Mixin" rel="nofollow">mixins</a>. <code>String.call(Number.call({}))</code> should give you an object with both string and number methods. I haven&#8217;t tested this, so I can&#8217;t promise it works.</p>
<p>So, returning to your conclusions from the examples, your explanations are spot on except that <code>null</code> really isn&#8217;t an object. I believe it&#8217;s a mistake in Javascript to return <code>"object"</code> from <code>typeof null</code>. Instead, I believe it is a primitive, not an object, and should return <code>null</code>.</p>
<p>The solution I was hinting at, and still need to blog about, is <a href="http://github.com/pr1001/typed.js" rel="nofollow">typed.js</a>, a better, &#8216;replacement&#8217; type system for Javascript. It will give you consistent, rational type information. For instance, <code>null</code> is always a <code>NullType</code> in my type system.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Collin Donahue-Oponski</title>
		<link>http://www.bubblefoundry.com/blog/2010/05/javascript-type-gotchas/comment-page-1/#comment-6234</link>
		<dc:creator>Collin Donahue-Oponski</dc:creator>
		<pubDate>Fri, 28 May 2010 18:13:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.bubblefoundry.com/?p=336#comment-6234</guid>
		<description>You had me going for a few minutes... and now I understand the difference between the typeof and instanceof operators.  :)

In javascript, a &quot;type&quot; is different from a &quot;class&quot; (the object it is an instance of).  So &quot;typeof&quot; will only return whether it is a number, boolean, string, or object.  The &quot;instanceof&quot; operator is meant to be used on objects, to figure out what kind of object it is.  So...

typeof null 
     // returns &quot;object&quot; because this variable is technically a null object
null instanceof null 
     // well, &quot;null&quot; isn&#039;t an object type (i.e. an type you defined with a constructor) 
     // I think what we meant was:
null instanceof Object
     // returns false, because null is not an instance -&gt; it is the lack of an instance
true instanceof Boolean 
     // returns false, because true is not an instance of an Object, it is a primitive type
     // That explains the last two examples too.

Thanks for the lesson!</description>
		<content:encoded><![CDATA[<p>You had me going for a few minutes&#8230; and now I understand the difference between the typeof and instanceof operators.  :)</p>
<p>In javascript, a &#8220;type&#8221; is different from a &#8220;class&#8221; (the object it is an instance of).  So &#8220;typeof&#8221; will only return whether it is a number, boolean, string, or object.  The &#8220;instanceof&#8221; operator is meant to be used on objects, to figure out what kind of object it is.  So&#8230;</p>
<p>typeof null<br />
     // returns &#8220;object&#8221; because this variable is technically a null object<br />
null instanceof null<br />
     // well, &#8220;null&#8221; isn&#8217;t an object type (i.e. an type you defined with a constructor)<br />
     // I think what we meant was:<br />
null instanceof Object<br />
     // returns false, because null is not an instance -&gt; it is the lack of an instance<br />
true instanceof Boolean<br />
     // returns false, because true is not an instance of an Object, it is a primitive type<br />
     // That explains the last two examples too.</p>
<p>Thanks for the lesson!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

