<?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>szeiger.de &#187; JavaScript</title>
	<atom:link href="http://szeiger.de/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://szeiger.de</link>
	<description>Stefan Zeiger's Software Development Weblog</description>
	<lastBuildDate>Tue, 29 Jun 2010 21:33:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Novocode Application Framework 0.4 released</title>
		<link>http://szeiger.de/blog/2008/05/08/naf-04-released/</link>
		<comments>http://szeiger.de/blog/2008/05/08/naf-04-released/#comments</comments>
		<pubDate>Thu, 08 May 2008 15:55:39 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://szeiger.de/blog/2008/05/08/naf-04-released/</guid>
		<description><![CDATA[I&#8217;d like to announce the release of Novocode Application Framework 0.4.
NAF provides a layer of MVC-based GUI components on top of SWT and JFace. A GUI is specified as a tree of NAF components which can then be instantiated as SWT controls. More than just a one to one wrapper of SWT component properties, NAF [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to announce the release of Novocode Application Framework 0.4.</p>
<p>NAF provides a layer of MVC-based GUI components on top of SWT and JFace. A GUI is specified as a tree of NAF components which can then be instantiated as SWT controls. More than just a one to one wrapper of SWT component properties, NAF includes resource management (e.g. shared images and fonts), simplified configuration (e.g. CSS-like size units for layout managers) and model binding. Component trees can be described in a simple XML notation or in JavaScript with an extended JSON format. Applications can also be written entirely in JavaScript. The resource management framework can be used to instantiate and  configure your own classes with a few simple annotations, and it is easily extended for other description or scripting languages.</p>
<p>It&#8217;s been a while since the last release which is in part due to design problems with the previous versions, which took me quite some time to resolve. The project still doesn&#8217;t have a catchier name <img src='http://szeiger.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  but it is now a lot easier to write custom components and the resource management framework can be extended for formats other than XML. The major new feature from an application programmer&#8217;s point of view is the JavaScript support.</p>
<p>NAF is licensed under EPL 1.0 and can be downloaded from <a href="http://novocode.com/naf/">http://novocode.com/naf/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2008/05/08/naf-04-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Super Constructor Calls in JavaScript</title>
		<link>http://szeiger.de/blog/2007/08/17/super-constructor-calls-in-javascript/</link>
		<comments>http://szeiger.de/blog/2007/08/17/super-constructor-calls-in-javascript/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 13:36:45 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://szeiger.de/blog/2007/08/17/super-constructor-calls-in-javascript/</guid>
		<description><![CDATA[I have previously shown a simple system for creating &#8220;classes&#8221; in JavaScript with Java-like inheritance. One limitation of this system is that calling a superclass constructor requires you to repeat the name of the superclass at the calling site:

extcls&#40;Point, function Rect&#40;x, y, w, h&#41;
&#123;
  Point.call&#40;this, x, y&#41;;
  this.w = w;
  this.h = [...]]]></description>
			<content:encoded><![CDATA[<p>I have <a href="http://szeiger.de/blog/2007/08/06/class-constructors-in-javascript/" title="Class Constructors in JavaScript">previously</a> shown a simple system for creating &#8220;classes&#8221; in JavaScript with Java-like inheritance. One limitation of this system is that calling a superclass constructor requires you to repeat the name of the superclass at the calling site:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">extcls<span style="color: #66cc66;">&#40;</span>Point, <span style="color: #003366; font-weight: bold;">function</span> Rect<span style="color: #66cc66;">&#40;</span>x, y, w, h<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  Point.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, x, y<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">w</span> = w;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">h</span> = h;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is both ugly and unnecessarily limiting. Furthermore, there is no guarantee that the superclass constructor is actually called. In Java, the compiler automatically inserts a <em>super()</em> call at the beginning of a constructor if there is no explicit call to a superclass constructor. This behavior cannot be easily duplicated in JavaScript but it should at least be possible to force an explicit superclass constructor call and throw an exception if such a call does not happen.</p>
<p>We cannot quite use Java&#8217;s syntax, either, because firstly, the <em>super</em> keyword is reserved in JavaScript and secondly, we need to invoke the super call through the <em>this</em> object, but we can get close by calling it <em>this.superc</em> (with the &#8220;c&#8221; standing for &#8220;constructor&#8221;). Here&#8217;s the <em>Rect</em> constructor using the new syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">extcls<span style="color: #66cc66;">&#40;</span>Point, <span style="color: #003366; font-weight: bold;">function</span> Rect<span style="color: #66cc66;">&#40;</span>x, y, w, h<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">superc</span><span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">w</span> = w;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">h</span> = h;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Where and how can this <em>superc</em> function be defined? The previous example with a base class and one subclass makes it look deceptively simple. Let&#8217;s add another class to the hierarchy:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">extcls<span style="color: #66cc66;">&#40;</span>Rect, <span style="color: #003366; font-weight: bold;">function</span> ColoredRect<span style="color: #66cc66;">&#40;</span>x, y, w, h, color<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">superc</span><span style="color: #66cc66;">&#40;</span>x, y, w, h<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">color</span> = color;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>When you create in instance of <em>ColoredRect</em>, the <em>this.superc()</em> call in <em>ColoredRect</em> has to call <em>Rect()</em> but the <em>this.superc()</em> call in <em>Rect</em> has to call </em>Point()</em>, and it&#8217;s the same <em>this</em> object they&#8217;re calling it on! The only way to achieve this is by reassigning <em>superc</em> to the correct function before each constructor is called.</p>
<p>Fortunately, this is possible because <em>superc</em> is always called from the pseudo-constructors that are passed to <em>extcls</em> and these pseudo-constructors are always called from real constructors which are created on the fly by <em>extcls</em>. We can simply assign the correct <em>superc</em> function in the generated constructors before invoking the pseudo-constructors. And we can make sure that <em>superc</em> has been called by having it set a local flag and checking that flag when the pseudo-constructor returns. Here&#8217;s the updated <em>extcls</em> function with the new features:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> extcls<span style="color: #66cc66;">&#40;</span>parent, init<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> cons = <span style="color: #003366; font-weight: bold;">function</span> cons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> called;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">superc</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span> 
      parent.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, arguments<span style="color: #66cc66;">&#41;</span>;
      called = <span style="color: #003366; font-weight: bold;">true</span>;
    <span style="color: #66cc66;">&#125;</span>;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>arguments.<span style="color: #006600;">length</span> == <span style="color: #CC0000;">1</span> &amp;&amp; arguments<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span> === cons<span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;
    init.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>parent &amp;&amp; parent.__init &amp;&amp; !called<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;superc() was not called in &quot;</span>+init.<span style="color: #000066;">name</span>+<span style="color: #3366CC;">&quot;()&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>;
  cons.<span style="color: #006600;">toString</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;[Generated constructor for class &quot;</span>+init.<span style="color: #000066;">name</span>+<span style="color: #3366CC;">&quot;]&quot;</span>; <span style="color: #66cc66;">&#125;</span>;
  cons.__init = init;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>parent<span style="color: #66cc66;">&#41;</span>
    cons.<span style="color: #006600;">prototype</span> = parent.__init ?
      <span style="color: #003366; font-weight: bold;">new</span> parent<span style="color: #66cc66;">&#40;</span>parent<span style="color: #66cc66;">&#41;</span> : <span style="color: #003366; font-weight: bold;">new</span> parent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>init.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span> self<span style="color: #66cc66;">&#91;</span>init.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#93;</span> = cons;
  <span style="color: #000066; font-weight: bold;">return</span> cons;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Note that the <em>superc()</em> call may be omitted if no parent class has been given (in which case <em>object</em> is used) or if the parent class is a plain JavaScript constructor that was not defined by <em>defcls</em> or <em>extcls</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2007/08/17/super-constructor-calls-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Class Constructors in JavaScript</title>
		<link>http://szeiger.de/blog/2007/08/06/class-constructors-in-javascript/</link>
		<comments>http://szeiger.de/blog/2007/08/06/class-constructors-in-javascript/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 21:17:11 +0000</pubDate>
		<dc:creator>Stefan Zeiger</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://szeiger.de/blog/2007/08/06/class-constructors-in-javascript/</guid>
		<description><![CDATA[If your previous experience with object-oriented programming has been in &#8220;classical&#8221; OOP languages like Java or C++, JavaScript&#8217;s prototype object system probably seemed strange at first. When I picked up JavaScript, it was one of the key parts for me to learn. I just wanted to see what possibilities it affords you and how a [...]]]></description>
			<content:encoded><![CDATA[<p>If your previous experience with object-oriented programming has been in &#8220;classical&#8221; OOP languages like Java or C++, JavaScript&#8217;s prototype object system probably seemed strange at first. When I picked up JavaScript, it was one of the key parts for me to learn. I just wanted to see what possibilities it affords you and how a traditional class model with virtual methods, constructors, hidden state and inheritance could be mapped to JavaScript.</p>
<p>It turns out that a single class (if you can call it that in a classless language) is easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> Point<span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">x</span> = x;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">y</span> = y;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
Point.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;(&quot;</span> + <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">x</span> + <span style="color: #3366CC;">&quot;,&quot;</span> + <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">y</span> + <span style="color: #3366CC;">&quot;)&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> p = <span style="color: #003366; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">1</span>,<span style="color: #CC0000;">2</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>p.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Running this code gives the expected result: <em>p = (1,2)</em>.</p>
<p>Now let&#8217;s write a class <em>Rect</em> which extends <em>Point</em> and adds two more fields, for the width and the height of the rectangle. The standard approach you&#8217;ll find in JavaScript tutorials is to call the <em>Point</em> function to create a prototype for <em>Rect:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> Rect<span style="color: #66cc66;">&#40;</span>x, y, w, h<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  Point.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, x, y<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">w</span> = w;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">h</span> = h;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
Rect.<span style="color: #006600;">prototype</span> = <span style="color: #003366; font-weight: bold;">new</span> Point<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> r = <span style="color: #003366; font-weight: bold;">new</span> Rect<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">3</span>,<span style="color: #CC0000;">4</span>,<span style="color: #CC0000;">5</span>,<span style="color: #CC0000;">6</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>r.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Since we haven&#8217;t provided a <em>toString</em> method for <em>Rect</em>, the call to <em>toString</em> is delegated to <em>Point</em>&#8217;s original implementation, so the result is <em>(3,4)</em>.</p>
<p>Line 8 highlights the problem with this strategy: You have to call the superclass constructor <em>to create the prototype!</em> In many cases this is not an issue. In fact, in the <em>Rect</em> example above, the constructor will simply assign the value <em>undefined</em> to the fields <em>x</em> and <em>y</em> of the <em>Rect</em> prototype, but if you want to be able to treat the superclass as a black box, you need a way of subclassing it without calling its constructor on a dummy prototype object.</p>
<p>Unfortunately, you <em>must</em> call the constructor to create the prototype, but you can move the initialization code to a different function instead. The actual constructor can then be created automatically by this helper function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> extcls<span style="color: #66cc66;">&#40;</span>parent, init<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> cons = <span style="color: #003366; font-weight: bold;">function</span> cons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>arguments.<span style="color: #006600;">length</span> == <span style="color: #CC0000;">1</span> &amp;&amp; arguments<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span> === cons<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">return</span>;
    init.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, arguments<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>;
  cons.<span style="color: #006600;">toString</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;[Generated constructor for class &quot;</span>+init.<span style="color: #000066;">name</span>+<span style="color: #3366CC;">&quot;]&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>;
  cons.__init = init;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>parent<span style="color: #66cc66;">&#41;</span>
    cons.<span style="color: #006600;">prototype</span> = parent.__init ?
      <span style="color: #003366; font-weight: bold;">new</span> parent<span style="color: #66cc66;">&#40;</span>parent<span style="color: #66cc66;">&#41;</span> : <span style="color: #003366; font-weight: bold;">new</span> parent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>init.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span> self<span style="color: #66cc66;">&#91;</span>init.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#93;</span> = cons;
  <span style="color: #000066; font-weight: bold;">return</span> cons;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> defcls<span style="color: #66cc66;">&#40;</span>init<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> extcls<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span>, init<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The <em>Point</em> and <em>Rect</em> classes can now be defined like this using <em>defcls</em> and <em>extcls:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript">defcls<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> Point<span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">x</span> = x;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">y</span> = y;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
Point.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;(&quot;</span> + <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">x</span> + <span style="color: #3366CC;">&quot;,&quot;</span> + <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">y</span> + <span style="color: #3366CC;">&quot;)&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
extcls<span style="color: #66cc66;">&#40;</span>Point, <span style="color: #003366; font-weight: bold;">function</span> Rect<span style="color: #66cc66;">&#40;</span>x, y, w, h<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  Point.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, x, y<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">w</span> = w;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">h</span> = h;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Another problem remains with this code. On line 18, the <em>Rect</em> constructor has to call its super constructor <em>Point()</em> explicitly by name, and the syntax is similar when you override a method and want to call a method of the superclass. What&#8217;s missing here is Java&#8217;s <em>super</em> keyword. But I&#8217;ll leave that for another post.</p>
]]></content:encoded>
			<wfw:commentRss>http://szeiger.de/blog/2007/08/06/class-constructors-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
