<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Burcu Dogan&#039;s Blog &#187; javascript</title>
	<atom:link href="http://burcudogan.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://burcudogan.com</link>
	<description>Burcu Dogan is a software engineer, a technologist and an open source enthusiastic.</description>
	<lastBuildDate>Thu, 05 Jan 2012 08:01:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='burcudogan.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Burcu Dogan&#039;s Blog &#187; javascript</title>
		<link>http://burcudogan.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://burcudogan.com/osd.xml" title="Burcu Dogan&#039;s Blog" />
	<atom:link rel='hub' href='http://burcudogan.com/?pushpress=hub'/>
		<item>
		<title>Implementing Interfaces/Protocols in JavaScript?</title>
		<link>http://burcudogan.com/2010/09/10/implementing-interfacesprotocols-in-javascript/</link>
		<comments>http://burcudogan.com/2010/09/10/implementing-interfacesprotocols-in-javascript/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 15:17:18 +0000</pubDate>
		<dc:creator>Burcu Dogan</dc:creator>
				<category><![CDATA[Regular]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.burcudogan.com/?p=715</guid>
		<description><![CDATA[Several years later, I decided to read Pro JavaScript Design Patterns by Harmes and Diaz again. This book has almost become controversial for its introductory section which presents a suggestion to implement interfaces in JavaScript. Pro JS Design Patterns implements an Interface object, simply by attaching the several method attributes to an object during creation. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=burcudogan.com&amp;blog=21440216&amp;post=715&amp;subd=burcudo&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Several years later, I decided to read <a href="http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X">Pro JavaScript Design Patterns by Harmes and Diaz</a> again. This book has almost become controversial for its introductory section which presents a suggestion to implement <em>interfaces in JavaScript</em>.</p>
<p>Pro JS Design Patterns implements an Interface object, simply by attaching the several method attributes to an object during creation. The interface class is more likely to be a Factory class since it creates a new class definition based on the input parameters.</p>
<pre>// Constructor.
var Interface = function(name, methods) {
    this.name = name;
    this.methods = [];
    for(var i = 0, len = methods.length; i &lt; len; i++) {
        this.methods.push(methods[i]);
    }
};
// create Widget class which implements an interface with two methods: addSubView and removeFromSuper.
var Widget = new Interface('Widget', ['addSubView', 'removeFromSuper']);</pre>
<p>Once you initialize Widget, you ensure that Widget is implementing <em>addSubView</em> and <em>removeFromSuper</em> methods. This is a methodology/concept book is presenting. In this blog post, I’ll try to explain you why conceptually it makes no sense to implement an Interface object in JS.</p>
<h2>There is no “before run-time”</h2>
<p>Dynamic languages are forming the classes at the run-time. Interfaces are invented to find failures during compiling time &#8212; long before code is being run. If you develop control methodologies or test against a structure, it might be very pointless due to the fact they are going to be executed in run-time. If your situation is not critical, <strong>let it fail on actual code</strong> during the test. Nevertheless, you can always check if required methods are implemented by the instance and raise flags if something has gone bad for critical operations or not-trusted objects comes from an AJAX request for example.<br />
If you truly want to make “before run-time” checks, your problem turns out to be a<em> unit-testing</em> one. You can simply write external test code to consume your JS library and check if your object is implementing your set of methods. This is absolutely is not a solution since you are able to modify object structure during the execution (see next section). You, again, can’t be sure your code is going to break it all or not.</p>
<h2>Hierarchy or functionality changes at run-time</h2>
<p>Almost any object is changeable during run-time &#8212; although there are some exceptions with [global] object in browser implementations since window is the [global] object and modification is a security problem . Hierarchy may change, properties may change. So, there is again no guarantee that a Factory created object will agree a certain protocol during the run-time.</p>
<h2>The cost</h2>
<p>Nothing comes for free. Factory like initialization is an overhead, huge or small, it’s still overhead. It requires extra iterations over objects during creation. And you’re required to iterate over same properties while implementing the methods.</p>
<p>Consequently, there is almost no practical point to create classes from an Interface factory at all. If your code is getting large, write automated tests and find ways to share schema around the developers more efficiently.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/burcudo.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/burcudo.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/burcudo.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/burcudo.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/burcudo.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/burcudo.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/burcudo.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/burcudo.wordpress.com/715/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=burcudogan.com&amp;blog=21440216&amp;post=715&amp;subd=burcudo&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://burcudogan.com/2010/09/10/implementing-interfacesprotocols-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e7526ec3e801f8ba99f6746498a154a6?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">thejbf</media:title>
		</media:content>
	</item>
	</channel>
</rss>
