<?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>CodeUtopia - The blog of Jani Hartikainen &#187; Books</title>
	<atom:link href="http://codeutopia.net/blog/tag/books/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeutopia.net/blog</link>
	<description>Software development with a focus on web-related technologies</description>
	<lastBuildDate>Thu, 02 Sep 2010 14:25:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>NetBeans Platform 6.9 Developer&#8217;s Guide review coming soon</title>
		<link>http://codeutopia.net/blog/2010/09/02/netbeans-platform-6-9-developer-guide-review-coming-soon/</link>
		<comments>http://codeutopia.net/blog/2010/09/02/netbeans-platform-6-9-developer-guide-review-coming-soon/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 14:24:35 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=363</guid>
		<description><![CDATA[I have recently received a review copy of Jürgen Petri&#8217;s book, NetBeans Platform 6.9 Developer&#8217;s Guide, and I will be posting a review of it after I finish reading it in the coming weeks. You can check the book out on Packt&#8217;s store page. Some of you may recall I was also doing a review [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float:right" src="http://codeutopia.net/blog-images/nb69devguide_s.jpg" alt="Cover" /> I have recently received a review copy of Jürgen Petri&#8217;s book, <a href="https://www.packtpub.com/netbeans-platform-6-9-developers-guide/book">NetBeans Platform 6.9 Developer&#8217;s Guide</a>, and I will be posting a review of it after I finish reading it in the coming weeks.</p>
<p>You can check the book out on <a href="https://www.packtpub.com/netbeans-platform-6-9-developers-guide/book">Packt&#8217;s store page</a>.</p>
<p>Some of you may recall I was also doing a review of <a href="http://codeutopia.net/blog/2009/10/12/going-to-review-zend-framework-18-web-application-development/">Zend Framework 1.8 Web Application Development</a>. This review was delayed becayse of me being the usual me and forgetting things, so I&#8217;d like to apologize to anyone who has been waiting for it. I&#8217;m going to post my review of it next week &#8211; I already have it written too and it just needs minor edits, so this time I&#8217;m not going to forget about it <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/09/02/netbeans-platform-6-9-developer-guide-review-coming-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Going to review Zend Framework 1.8 Web Application Development</title>
		<link>http://codeutopia.net/blog/2009/10/12/going-to-review-zend-framework-18-web-application-development/</link>
		<comments>http://codeutopia.net/blog/2009/10/12/going-to-review-zend-framework-18-web-application-development/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 15:16:37 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2009/10/12/going-to-review-zend-framework-18-web-application-development/</guid>
		<description><![CDATA[I was contacted by Packt Publishing to review a new Zend Framework book, Zend Framework 1.8 Web Application Development. I have previously reviewed Object-Oriented JavaScript, also published by Packt, which was a quality title so I’m looking forward to reading this ZF book. The book is written by Keith Pope, a web developer/project manager from [...]]]></description>
			<content:encoded><![CDATA[<p>I was contacted by Packt Publishing to review a new Zend Framework book, <a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/book">Zend Framework 1.8 Web Application Development</a>. I have previously reviewed <a href="http://codeutopia.net/blog/2009/03/11/object-oriented-javascript-book-review/">Object-Oriented JavaScript</a>, also published by Packt, which was a quality title so I’m looking forward to reading this ZF book.</p>
<p>The book is written by Keith Pope, a web developer/project manager from the UK. I don’t yet have the book and reading it will probably take a while, so in the meantime, do check out <a href="http://thepopeisdead.com/">Keith&#8217;s blog</a> which I have been following for a while already. Amongst other things, he has posted a free chapter from the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2009/10/12/going-to-review-zend-framework-18-web-application-development/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Object-Oriented JavaScript book review</title>
		<link>http://codeutopia.net/blog/2009/03/11/object-oriented-javascript-book-review/</link>
		<comments>http://codeutopia.net/blog/2009/03/11/object-oriented-javascript-book-review/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 17:14:50 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2009/03/11/object-oriented-javascript-book-review/</guid>
		<description><![CDATA[I was recently contacted by Packt Publishing, and asked if I wanted to review a book they published &#8211; Object-Oriented JavaScript, written by Stoyan Stefanov. I hadn&#8217;t reviewed books before, but I decided to give it a shot! As stated on Packt&#8217;s website, by reading the book you will learn: Create scalable, reusable high-quality JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently contacted by <a href="http://www.packtpub.com/">Packt Publishing</a>, and asked if I wanted to review a book they published &#8211; <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">Object-Oriented JavaScript</a>, written by Stoyan Stefanov. I hadn&#8217;t reviewed books before, but I decided to give it a shot!</p>
<p>As stated on Packt&#8217;s website, by reading the book you will learn:</p>
<blockquote><p>
Create scalable, reusable high-quality JavaScript applications and libraries </p>
<ul>
<li>Learn to think in JavaScript, the language of the web browser</li>
<li>Object-oriented programming made accessible and understandable to web developers </li>
<li>Do it yourself: experiment with examples that can be used in your own scripts</li>
<li>Write better, more maintainable JavaScript code</li>
</ul>
</blockquote>
<p>It also says:</p>
<blockquote><p>After reading this book, you&#8217;ll be prepared to ace your JavaScript job interview and even impress with some bits that the interviewer maybe didn&#8217;t know. You should read this book if you want to be able to take your JavaScript skills to a new level of sophistication.</p></blockquote>
<p>which would definitely be a benefit, don&#8217;t you think so?</p>
<p><b>Does the book deliver all this?</b></p>
<p><span id="more-213"></span></p>
<h3>The contents</h3>
<p>The book is divided into 8 chapters, four of which deal with the basic features of the JavaScript language, and the other four dive deeper into the language and the actual object-orientation parts like inheritance and design patterns. </p>
<p>The book uses Firebug as the main &#8220;development environment&#8221;, which I think is a good thing. Firebug is an important tool for a JavaScript programmer, and it works quite well for the task of teaching and learning too.</p>
<p>The book uses tip boxes, which contain best practices, common mistakes or other useful information, and the code samples in the book are consistent in style and display use of good practices.</p>
<p>Each chapter also contains exercises in the end, which help you to learn the concepts introduced in the chapter.</p>
<h3>The JavaScript basics</h3>
<p><b>The first chapter</b> begins by looking a bit into what JavaScript is: Where it came from, where it is now and also a bit about the future. It serves as a nice look into the language&#8217;s roots. It also introduces JavaScript and OOP concepts, and compares them with other languages.</p>
<p><b>Next, in the second chapter</b>, the book looks more into JavaScript&#8217;s types, expressions and constructs. Everything is explained thoroughly, and using language that even beginner programmers should understand. It even explains terminology for words like &#8220;operator&#8221; and &#8220;operand&#8221;, so even if you&#8217;re not familiar with these terms &#8211; for example if english isn&#8217;t your native language &#8211; you won&#8217;t have any problems with the upcoming chapters. It also explains more obscure syntax like the exponential &#8220;2e-5&#8243; and special values NaN, Infinity and others.</p>
<p><b>The third chapter</b> is all about JavaScript functions, again starting with basics, terminology and explaining things very thoroughly. It goes into more detail and more complex examples about functions too: anonymous functions, inner functions, etc.</p>
<p>As an added bonus, this chapter looks into some common JavaScript built in functions.</p>
<p><b>Chapter four</b> introduces objects, which have been refered to a bit earlier in the book as well. As usual, it&#8217;s very clear and everything is explained well. It talks about how JavaScript&#8217;s object oriented features differ from other languages and provides some examples which are useful if you&#8217;re familiar with some &#8220;classical&#8221; OOP language. Like with the previous chapter, this one also shows you how to use built in JavaScript objects such as Number and Date.</p>
<h3>More in-depth</h3>
<p><b>The fifth chapter</b> talks about the prototype property. You can <a href="http://codeutopia.net/blog/2009/03/04/using-prototype-property-in-javascript/" title="Using Prototype property in JavaScript">read most of this chapter online here at my blog</a>. There&#8217;s a bit more towards the end of the chapter which isn&#8217;t included in the post, though. This chapter is one of the most useful parts in the book for readers who are more experienced in JavaScript.</p>
<p><b>Chapter six</b> is again a chapter looking deeper into JavaScript: inheritance. It explains various inheritance styles possible in JavaScript, such as prototype chaining and pseudo-classical inheritance. Also talks a bit about Yahoo! UI Framework and the Prototype library.</p>
<p><b>The seventh chapter</b> is dedicated to the browser: The browser object model and the document object model. It covers cross browser compatible features of the BOM and DOM, like working with DOM nodes and using XMLHttpRequest. It also talks about the event model used in the browser, and some help on how to make things work in IE. It does not go into very great detail, but serves as a good primer to working with the browser&#8217;s features.</p>
<p><b>The final chapter</b> looks into coding and design patterns. This one covers common things you see in modern JavaScript code, such as namespaces and initialization patterns. It also shows how to achieve public and private functions, and chaining, which is very common in jQuery. This chapter is very good, especially as many frameworks like Dojo and YUI use JavaScript in ways which can be confusing to beginners, but after reading this, you should understand them quite well.</p>
<h3>Appendixes</h3>
<p>The book also has four appendixes: A list of JavaScript reserved words, a list of builtin functions with examples, a list of objects with examples and a JavaScript regex syntax index.</p>
<h3>Good things</h3>
<p>The book is very thorough in explaining how JavaScript works. The first four chapters make the book suitable even for those who haven&#8217;t never used JavaScript before. They also contain some tidbits which more experienced programmers might not know about. The last four chapters make the book useful for people who are familiar with JS, and even those who are quite experienced in it can gain some new insights, for example about the prototype property or inheritance. </p>
<p>To highlight some good parts:</p>
<ul>
<li>The first chapter shows a Firefox &#8220;strict&#8221; JavaScript parsing mode I did not know about</li>
<li>The third chapter explains &#8220;self executing function&#8221; syntax, (function(){ &#8230; })();, which used to confuse me a lot some years back</li>
<li>The chapters on prototypes, inheritance and patterns are excellent, and they had various small things I didn&#8217;t know about</li>
</ul>
<h3>Bad things</h3>
<p>It&#8217;s difficult to find bad things about this book. Maybe the fact that I only have it as a PDF? </p>
<p>There were some small places for improvement, though. In the second chapter it might have been a good idea to mention that lazy evaluation in conditionals is also known as &#8220;short circuiting&#8221;. </p>
<p>Also, the third chapter code examples displaying inner and outer functions used f() and n() as the function names. It would probably have been a little better to use descriptive naming like outer() and inner(), so you wouldn&#8217;t have to look at the code example to see which one is which.</p>
<p>If you want to learn more about Ajax or DHTML-style tricks, this book may not be for you. While the browser environment chapter is good as an introduction, and you can definitely start using JavaScript on websites based on it, it doesn&#8217;t go into more advanced things.</p>
<h3>The bottom line</h3>
<p>To go back to the initial question, &#8220;does the book deliver?&#8221;, the answer is definitely <b>yes</b>. The book delivers all it promises, and after reading it, you will be able to impress interviewers.</p>
<p>All in all, the book is a great read for anyone who wants to learn JavaScript, or improve their existing skills in the language. The book is most useful for beginner to intermediate level JavaScripters, but may also contain some useful things if you&#8217;re more experienced.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2009/03/11/object-oriented-javascript-book-review/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Prototype Property in JavaScript</title>
		<link>http://codeutopia.net/blog/2009/03/04/using-prototype-property-in-javascript/</link>
		<comments>http://codeutopia.net/blog/2009/03/04/using-prototype-property-in-javascript/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 19:24:36 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2009/03/04/using-prototype-property-in-javascript/</guid>
		<description><![CDATA[The following topics are discussed in this article: Every function has a prototype property and it contains an object, Adding properties to the prototype object, Using the properties added to the prototype, The difference between own properties and properties of the prototype, __proto__, the secret link every object keeps to its prototype, Methods such as isPrototypeOf(), hasOwnProperty(), and propertyIsEnumerable()]]></description>
			<content:encoded><![CDATA[<p>I have been asked to review the book &#8220;Object-Oriented JavaScript&#8221; by Stoyan Stefanov, and published by Packt Publishing. The review is not quite ready yet, but in the meantime, here&#8217;s some exclusive content from the book for you to have a look at.</p>
<p>The following topics are discussed in this article:</p>
<ul>
<li>Every function has a prototype property and it contains an object</li>
<li>Adding properties to the prototype object</li>
<li>Using the properties added to the prototype</li>
<li>The difference between own properties and properties of the prototype</li>
<li><i>__proto__</i>, the secret link every object keeps to its prototype</li>
<li>Methods such as <i>isPrototypeOf()</i>, <i>hasOwnProperty()</i>, and<br />
<i>propertyIsEnumerable()</i></li>
</ul>
<p><span id="more-209"></span></p>
<h1>The prototype Property</h1>
<p>The functions in JavaScript are objects and they contain methods and properties. Some<br />
of the common methods are <i>apply()</i> and <i>call()</i> and some of the common<br />
properties are <i>length</i> and <i>constructor</i>. Another property of the function<br />
objects is <i>prototype.</i></p>
<p>If you define a simple function <i>foo()</i> you can access its properties as you<br />
would do with any other object:</p>
<pre>
&gt;&gt;&gt;function foo(a, b){return a * b;}
&gt;&gt;&gt;foo.length
</pre>
<p><b>2</b></p>
<pre>
&gt;&gt;&gt;foo.constructor
</pre>
<p><b>Function()</b></p>
<p><i>prototype</i> is a property that gets created as soon as you define the function.<br />
Its initial value is an empty object.</p>
<pre>
&gt;&gt;&gt;typeof foo.prototype
</pre>
<p><b>&#8220;object&#8221;</b></p>
<p>It&#8217;s as if you added this property yourself like this:</p>
<pre>
&gt;&gt;&gt;foo.prototype = {}
</pre>
<p>You can augment this empty object with properties and methods. They won&#8217;t have any<br />
effect of the <i>foo()</i> function itself; they&#8217;ll only be used when you use<br />
<i>foo()</i>as a constructor.</p>
<h2>Adding Methods and Properties Using the Prototype</h2>
<p>Constructor functions can be used to create (construct) new objects. The main idea is<br />
that inside a function invoked with <i>new</i> you have access to the value <i>this</i>,<br />
which contains the object to be returned by the constructor. Augmenting (adding methods<br />
and properties to) <i>this</i> object is the way to add functionality to the object being<br />
created.</p>
<p>Let&#8217;s take a look at the constructor function <i>Gadget()</i> which uses <i>this</i><br />
to add two properties and one method to the objects it creates.</p>
<pre>
function Gadget(name, color) {
  this.name = name;
  this.color = color;
  this.whatAreYou = function(){
    return 'I am a ' + this.color + ' ' + this.name;
  }
}
</pre>
<p>Adding methods and properties to the <i>prototype</i> property of the constructor<br />
function is another way to add functionality to the objects this constructor produces.<br />
Let&#8217;s add two more properties, <i>price</i> and <i>rating</i>, and a <i>getInfo()</i><br />
method. Since <i>prototype</i> contains an object, you can just keep adding to it like<br />
this:</p>
<pre>
Gadget.prototype.price = 100;
Gadget.prototype.rating = 3;
Gadget.prototype.getInfo = function() {
  return 'Rating: ' + this.rating + ', price: ' + this.price;
};
</pre>
<p>Instead of adding to the <i>prototype</i> object, another way to achieve the above<br />
result is to overwrite the prototype completely, replacing it with an object of your<br />
choice:</p>
<pre>
Gadget.prototype = {
  price: 100,
  rating: 3,
  getInfo: function() {
    return 'Rating: ' + this.rating + ', price: ' + this.price;
  }
};
</pre>
<h2>Using the Prototype&#8217;s Methods and Properties</h2>
<p>All the methods and properties you have added to the prototype are directly available<br />
as soon as you create a new object using the constructor. If you create a newtoy object<br />
using the <i>Gadget()</i> constructor, you can access all the methods and properties<br />
already defined.</p>
<pre>
&gt;&gt;&gt; var newtoy = new Gadget('webcam', 'black');
&gt;&gt;&gt; newtoy.name;
</pre>
<p><b>&#8220;webcam&#8221;</b></p>
<pre>
&gt;&gt;&gt; newtoy.color;
</pre>
<p><b>&#8220;black&#8221;</b></p>
<pre>
&gt;&gt;&gt; newtoy.whatAreYou();
</pre>
<p><b>&#8220;I am a black webcam&#8221;</b></p>
<pre>
&gt;&gt;&gt; newtoy.price;
</pre>
<p><b>100</b></p>
<pre>
&gt;&gt;&gt; newtoy.rating;
</pre>
<p><b>3</b></p>
<pre>
&gt;&gt;&gt; newtoy.getInfo();
</pre>
<p><b>&#8220;Rating: 3, price: 100&#8243;</b></p>
<p>It&#8217;s important to note that the prototype is &#8220;live&#8221;. Objects are passed by reference<br />
in JavaScript, and therefore the prototype is not copied with every new object instance.<br />
What does this mean in practice? It means that you can modify the prototype at any time<br />
and all objects (even those created before the modification) will inherit the<br />
changes.</p>
<p>Let&#8217;s continue the example, adding a new method to the prototype:</p>
<pre>
Gadget.prototype.get = function(what) {
  return this[what];
};
</pre>
<p>Even though newtoy was created <i>before</i> the <i>get()</i> method was defined,<br />
<i>newtoy</i> will still have access to the new method:</p>
<pre>
&gt;&gt;&gt; newtoy.get('price');
</pre>
<p><b>100</b></p>
<pre>
&gt;&gt;&gt; newtoy.get('color');
</pre>
<p><b>&#8220;black&#8221;</b></p>
<h2>Own Properties versus prototype Properties</h2>
<p>In the example above <i>getInfo()</i> used this internally to address the object. It<br />
could&#8217;ve also used <i>Gadget.prototype</i> to achieve the same result:</p>
<pre>
Gadget.prototype.getInfo = function() {
  return 'Rating: ' + Gadget.prototype.rating + ', price: ' + Gadget.prototype.price;
};
</pre>
<p>What&#8217;s is the difference? To answer this question, let&#8217;s examine how the prototype<br />
works in more detail.</p>
<p>Let&#8217;s again take our <i>newtoy</i> object:</p>
<pre>
&gt;&gt;&gt; var newtoy = new Gadget('webcam', 'black');
</pre>
<p>When you try to access a property of <i>newtoy</i>, say <i>newtoy.name</i> the<br />
JavaScript engine will look through all of the properties of the object searching for one<br />
called <i>name</i> and, if it finds it, will return its value.</p>
<pre>
&gt;&gt;&gt; newtoy.name
</pre>
<p><b>&#8220;webcam&#8221;</b></p>
<p>What if you try to access the <i>rating</i> property? The JavaScript engine will<br />
examine all of the properties of newtoy and will not find the one called rating. Then the<br />
script engine will identify the prototype of the constructor function used to create this<br />
object (same as if you do <i>newtoy.constructor.prototype</i>). If the property is found<br />
in the prototype, this property is used.</p>
<pre>
&gt;&gt;&gt; newtoy.rating
</pre>
<p><b>3</b></p>
<p>This would be the same as if you accessed the prototype directly. Every object has a<br />
constructor property, which is a reference to the function that created the object, so in<br />
our case:</p>
<pre>
&gt;&gt;&gt; newtoy.constructor
</pre>
<p><b>Gadget(name, color)</b></p>
<pre>
&gt;&gt;&gt; newtoy.constructor.prototype.rating
</pre>
<p><b>3</b></p>
<p>Now let&#8217;s take this lookup one step further. Every object has a constructor. The<br />
prototype is an object, so it must have a constructor too. Which in turn has a prototype.<br />
In other words you can do:</p>
<pre>
&gt;&gt;&gt; newtoy.constructor.prototype.constructor
</pre>
<p><b>Gadget(name, color)</b></p>
<pre>
&gt;&gt;&gt; newtoy.constructor.prototype.constructor.prototype
</pre>
<p><b>Object price=100 rating=3</b></p>
<p>This might go on for a while, depending on how long the prototype chain is, but you<br />
eventually end up with the built-in <i>Object()</i> object, which is the highest-level<br />
parent. In practice, this means that if you try <i>newtoy.toString()</i> and newtoy<br />
doesn&#8217;t have an own <i>toString()</i> method and its prototype doesn&#8217;t either, in the end<br />
you&#8217;ll get the Object&#8217;s <i>toString()</i></p>
<pre>
&gt;&gt;&gt; newtoy.toString()
</pre>
<p><b>&#8220;[object Object]&#8220;</b></p>
<h2>Overwriting Prototype&#8217;s Property withOwn Property</h2>
<p>As the above discussion demonstrates, if one of your objects doesn&#8217;t have a certain<br />
property of its own, it can use one (if exists) somewhere up the prototype chain. What if<br />
the object does have its own property and the prototype also has one with the same name?<br />
The own property takes precedence over the prototype&#8217;s.</p>
<p>Let&#8217;s have a scenario where a property name exists both as an own property and as a<br />
property of the prototype object:</p>
<pre>
function Gadget(name) {
  this.name = name;
}
Gadget.prototype.name = 'foo';
</pre>
<p><b>&#8220;foo&#8221;</b></p>
<p>Creating a new object and accessing its name property gives you the object&#8217;s<br />
own<i>name</i> property.</p>
<pre>
&gt;&gt;&gt; var toy = new Gadget('camera');
&gt;&gt;&gt; toy.name;
</pre>
<p><b>&#8220;camera&#8221;</b></p>
<p>If you delete this property, the prototype&#8217;s property with the same name&#8221;shines<br />
through&#8221;:</p>
<pre>
&gt;&gt;&gt; delete toy.name;
</pre>
<p><b>true</b></p>
<pre>
&gt;&gt;&gt; toy.name;
</pre>
<p style="font-weight: bold;">&#8220;foo&#8221;</p>
<p>Of course, you can always re-create the object&#8217;s own property:</p>
<pre>
&gt;&gt;&gt; toy.name = 'camera';
&gt;&gt;&gt; toy.name;
</pre>
<p><b>&#8220;camera&#8221;</b></p>
<hr size="1" color="#FF9933" noshade="noshade" />
<p align="left"><b><a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book"><font size="2" face="Verdana">Object-Oriented JavaScript</font></a></b></p>
<table id="table3" border="0" cellpadding="4" cellspacing="0">
<tbody>
<tr>
<td width="48"><img src="http://images.packtpub.com/images/114x140/1847194141.png" title="Object-Oriented JavaScript" alt="Object-Oriented JavaScript" width="70" border="1" height="90" /></td>
<td valign="top">
<font size="2" face="Verdana">Create scalable, reusable high-quality JavaScript applications and libraries</font></p>
<ul>
<li><font size="2" face="Verdana">Learn to think in JavaScript, the language of the web browser</font></li>
<li><font size="2" face="Verdana">Object-oriented programming made accessible and understandable to web developers</font></li>
<li><font size="2" face="Verdana">Do it yourself: experiment with examples that can be used in your own scripts</font></li>
<li><font size="2" face="Verdana">Write better, more maintainable JavaScript code</font></li>
</ul>
<p><font size="2" face="Verdana">For more information, please visit:<br />
<a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">http://www.PacktPub.com/object-oriented-javascript-applications-libraries/book</a></font></p>
</td>
</tr>
</tbody>
</table>
<hr size="1" color="#FF9933" noshade="noshade" />
<h2>Enumerating Properties</h2>
<p>If you want to list all properties of an object, you can use a <i>for-in</i> loop. In<br />
Chapter 2, you saw how you could loop through all the elements of an array:</p>
<pre>
var a = [1, 2, 3];
for (var i in a) {
  console.log(a[i]);
}
</pre>
<p>Arrays are objects, so you can expect that the <i>for-in</i> loop works for objects<br />
too:</p>
<pre>
var o = {p1: 1, p2: 2};
for (var i in o) {
  console.log(i + '=' + o[i]);
}
</pre>
<p>This produces:</p>
<p><b>p1=1</b></p>
<p><b>p2=2</b></p>
<p>There are some details to be aware of:</p>
<ul>
<li>Not all properties show up in a <i>for-in</i> loop. For example, the length (for<br />
arrays) and constructor properties will not show up. The properties that do show up are<br />
called <i>enumerable</i>. You can check which ones are enumerable with the help of the<br />
<i>propertyIsEnumerable()</i> method that everyobject provides.</li>
<li>Prototypes that come through the prototype chain will also show up, provided they are<br />
enumerable. You can check if a property is an own property versus prototype&#8217;s using the<br />
<i>hasOwnProperty()</i> method.</li>
<li><i>propertyIsEnumerable()</i> will return <i>false</i> for all of the prototype&#8217;s<br />
properties, even those that are enumerable and will show up in the<i>for-in</i><br />
loop.</li>
</ul>
<p>Let&#8217;s see these methods in action. Take this simplified version of<br />
<i>Gadget()</i>:</p>
<pre>
function Gadget(name, color) {
  this.name = name;
  this.color = color;
  this.someMethod = function(){return 1;}
}
Gadget.prototype.price = 100;
Gadget.prototype.rating = 3;
</pre>
<p>Creating a new object:</p>
<pre>
var newtoy = new Gadget('webcam', 'black');
</pre>
<p>Now if you loop using a <i>for-in</i>, you see of the object&#8217;s all properties,<br />
including those that come from the prototype:</p>
<pre>
for (var prop in newtoy) {
  console.log(prop + ' = ' + newtoy[prop]);
}
</pre>
<p>The result also contains the object&#8217;s methods (as methods are just properties that<br />
happen to be functions):</p>
<p><b>name = webcam</b></p>
<p><b>color = black</b></p>
<p><b>someMethod = function () { return 1; }</b></p>
<p><b>price = 100</b></p>
<p><b>rating = 3</b></p>
<p>If you want to distinguish between the object&#8217;s own properties versus the prototype&#8217;s<br />
properties, use <i>hasOwnProperty()</i>. Try first:</p>
<pre>
&gt;&gt;&gt; newtoy.hasOwnProperty('name')
</pre>
<p><b>true</b></p>
<pre>
&gt;&gt;&gt; newtoy.hasOwnProperty('price')
</pre>
<p><b>false</b></p>
<p>Let&#8217;s loop again, but showing only own properties:</p>
<pre>
for (var prop in newtoy) {
  if (newtoy.hasOwnProperty(prop)) {
   console.log(prop + '=' + newtoy[prop]);
  }
}
</pre>
<p>The result:</p>
<p><b>name=webcam</b></p>
<p><b>color=black</b></p>
<p><b>someMethod=function () { return 1; }</b></p>
<p>Now let&#8217;s try <i>propertyIsEnumerable()</i>. This method returns true for own<br />
properties that&nbsp;are&nbsp;not built-in:</p>
<pre>
&gt;&gt;&gt; newtoy.propertyIsEnumerable('name')
</pre>
<p><b>true</b></p>
<p>Most built-in properties and methods are not enumerable:</p>
<pre>
&gt;&gt;&gt; newtoy.propertyIsEnumerable('constructor')
</pre>
<p><b>false</b></p>
<p>Any properties coming down the prototype chain are not enumerable:</p>
<pre>
&gt;&gt;&gt; newtoy.propertyIsEnumerable('price')
</pre>
<p><b>false</b></p>
<p>Note, however, that such properties are enumerable if you reach the object contained<br />
in the prototype and invoke its <i>propertyIsEnumerable()</i>.</p>
<pre>
&gt;&gt;&gt; newtoy.constructor.prototype.propertyIsEnumerable('price')
</pre>
<p><b>true</b></p>
<h2>isPrototypeOf()</h2>
<p>Every object also gets the <i>isPrototypeOf()</i> method. This method tells you<br />
whether that specific object is used as a prototype of another object.</p>
<p>Let&#8217;s take a simple object monkey.</p>
<pre>
var monkey = {
  hair: true,
  feeds: 'bananas',
  breathes: 'air'
};
</pre>
<p>Now let&#8217;s create a <i>Human()</i> constructor function and set its prototype property<br />
to point to <i>monkey</i>.</p>
<pre>
function Human(name) {
  this.name = name;
}
Human.prototype = monkey;
</pre>
<p>Now if you create a new <i>Human</i> object called <i>george</i> and ask: &#8220;Is<br />
<i>monkey</i> <i>george&#8217;s</i> prototype?&#8221;, you&#8217;ll get <i>true</i>.</p>
<pre>
&gt;&gt;&gt; var george = new Human('George');
&gt;&gt;&gt; monkey.isPrototypeOf(george)
</pre>
<p><b>true</b></p>
<h2>The Secret __proto__ Link</h2>
<p>As you know already, the prototype property will be consulted when you try to access a<br />
property that does not exist in the current object.</p>
<p>Let&#8217;s again have an object called <i>monkey</i> and use it as a prototype when<br />
creating objects with the <i>Human()</i> constructor.</p>
<pre>
var monkey = {
  feeds: 'bananas',
  breathes: 'air'
};
function Human() {}
Human.prototype = monkey;
</pre>
<p>Now let&#8217;s create a <i>developer</i> object and give it some properties:</p>
<pre>
var developer = new Human();
developer.feeds = 'pizza';
developer.hacks = 'JavaScript';
</pre>
<p>Now let&#8217;s consult some of the properties. <i>hacks</i> is a property of thedeveloper<br />
object.</p>
<pre>
&gt;&gt;&gt; developer.hacks
</pre>
<p><b>&#8220;JavaScript&#8221;</b></p>
<p><i>feeds</i> could also be found in the object.</p>
<pre>
&gt;&gt;&gt; developer.feeds
</pre>
<p><b>&#8220;pizza&#8221;</b></p>
<p><i>breathes</i> doesn&#8217;t exist as a property of the <i>developer</i> object, so the<br />
prototype is looked up, as if there is a secret link pointing to the prototype<br />
object.</p>
<pre>
&gt;&gt;&gt; developer.breathes
</pre>
<p><b>&#8220;air&#8221;</b></p>
<p>Can you get from the developer object to the prototype object? Well, you could, using<br />
constructor as the middleman, so having something like<br />
<i>developer.constructor.prototype</i> should point to <i>monkey</i>. The problem is that<br />
this is not very reliable, because constructor is more for informational purposes and can<br />
easily be overwritten at any time. You can overwrite it with something that&#8217;s not even an<br />
object and this will not affect the normal functioning of the prototype chain.</p>
<p>Let&#8217;s set the constructor property to some string:</p>
<pre>
&gt;&gt;&gt; developer.constructor = 'junk'
</pre>
<p><b>&#8220;junk&#8221;</b></p>
<p>It seems like the prototype is now all messed up:</p>
<pre>
&gt;&gt;&gt; typeof developer.constructor.prototype
</pre>
<p><b>&#8220;undefined&#8221;</b></p>
<p>&#8230;but it isn&#8217;t, because the developer still breathes <b>&#8220;air&#8221;</b>:</p>
<pre>
&gt;&gt;&gt; developer.breathes
</pre>
<p><b>&#8220;air&#8221;</b></p>
<p>This shows that the secret link to the prototype still exists. The secret link is<br />
exposed in Firefox as the <i>__proto__</i> property (the word &#8220;proto&#8221; with two<br />
underscores before and two after).</p>
<pre>
&gt;&gt;&gt; developer.__proto__
</pre>
<p><b>Object feeds=bananas breathes=air</b></p>
<p>You can use this secret property for learning purposes but it&#8217;s not a good idea to use<br />
it in your real scripts, because it does not exist in Internet Explorer, so your scripts<br />
won&#8217;t be portable. For example, let&#8217;s say you have created a number of objects with<br />
<i>monkey</i> as a prototype and now you want to change something in all objects. You can<br />
change monkey and all instances will inherit the change:</p>
<pre>
&gt;&gt;&gt; monkey.test = 1
</pre>
<p><b>1</b></p>
<pre>
&gt;&gt;&gt; developer.test
</pre>
<p><b>1</b></p>
<p><i>__proto__</i> is not the same as prototype. <i>__proto__</i> is a property of the<br />
instances, whereas prototype is a property of the constructor functions.</p>
<pre>
&gt;&gt;&gt; typeof developer.__proto__
</pre>
<p><b>&#8220;object&#8221;</b></p>
<pre>
&gt;&gt;&gt; typeof developer.prototype
</pre>
<p><b>&#8220;undefined&#8221;</b></p>
<p>Once again, you should use <i>__proto__</i> only for learning or debugging<br />
purposes.</p>
<hr size="1" color="#FF9933" noshade="noshade" />
<p align="left"><b><a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book"><font size="2" face="Verdana">Object-Oriented JavaScript</font></a></b></p>
<table id="table3" border="0" cellpadding="4" cellspacing="0">
<tbody>
<tr>
<td width="48"><img src="http://images.packtpub.com/images/114x140/1847194141.png" title="Object-Oriented JavaScript" alt="Object-Oriented JavaScript" width="70" border="1" height="90" /></td>
<td valign="top">
<font size="2" face="Verdana">Create scalable, reusable high-quality JavaScript applications and libraries</font></p>
<ul>
<li><font size="2" face="Verdana">Learn to think in JavaScript, the language of the web browser</font></li>
<li><font size="2" face="Verdana">Object-oriented programming made accessible and understandable to web developers</font></li>
<li><font size="2" face="Verdana">Do it yourself: experiment with examples that can be used in your own scripts</font></li>
<li><font size="2" face="Verdana">Write better, more maintainable JavaScript code</font></li>
</ul>
<p><font size="2" face="Verdana">For more information, please visit:<br />
<a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">http://www.PacktPub.com/object-oriented-javascript-applications-libraries/book</a></font></p>
</td>
</tr>
</tbody>
</table>
<hr size="1" color="#FF9933" noshade="noshade" />
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2009/03/04/using-prototype-property-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
