<?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>Devlog &#187; Javascript</title>
	<atom:link href="http://devlog.info/cat/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://devlog.info</link>
	<description>One developers blog.</description>
	<lastBuildDate>Tue, 31 Aug 2010 18:45:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cross-domain AJAX with JSONP</title>
		<link>http://devlog.info/2010/03/10/cross-domain-ajax/</link>
		<comments>http://devlog.info/2010/03/10/cross-domain-ajax/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 23:42:54 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=139</guid>
		<description><![CDATA[Anyone who develops Javascript long enough undoubtedly runs into difficulties involving the various security features all browser vendors implement. These security features are a good thing -- they protect us from malicious users hijacking our browsing experience. But they can certainly cause some headaches. The security feature that presents the most difficulty for us as [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone who develops Javascript long enough undoubtedly runs into difficulties involving the various security features all browser vendors implement. These security features are a good thing -- they protect us from malicious users hijacking our browsing experience. But they can certainly cause some headaches. The security feature that presents the most difficulty for us as developers is the <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>.</p>
<p>In a nutshell, this policy prevents pages from two different domains from modifying each others properties, using XMLHttpRequest, setting cookies etc. For instance, Example.com and OtherExample.com can't get references to each others <code>document</code> properties and can't set cookies on each other. Additionally, Example.com can't use XMLHttpRequest (aka AJAX) to load a resource from OtherExample.com. This last bit is probably the biggest issue for developers today -- in todays world of open web services and mashups. How do you consume a web service with Javascript if you can't load the data properly?<span id="more-139"></span></p>
<h1>Solution 1: Use a server-side proxy</h1>
<p>The first way around this problem is to use a very simple server-side script that acts as a proxy to the web service. So instead of requesting AJAX from the remote site, you request it locally on your own domain through the proxy. The proxy itself has it's programming logic to send your request off to the remote site, gather the data, and then serve it back to you.</p>
<pre>.________________________.
| Client on YourSite.com |
'-\/---------/\----------'
  ||         ||
  ||_________||__.
  | AJAX Request |
  '---\/-----/\--'
      ||     ||
      ||_____||______________.
      | YourSite.com         |
      | (Ex. ajax_proxy.php) |
      '---\/-----/\----------'
          ||     ||
          ||_____||__________.
          | OtherSite.com    |
          '------------------'</pre>
<p>Since your server-side script has no qualms about fetching data from another domain, you can successfully proxy all AJAX like this without running into any trouble. And then since the client browser is fetching the data from the local domain (even though you're making another request behind the scenes), it doesn't violate the same origin policy.</p>
<p>An example proxy script might look something like this (maybe a little more complex if you're handling POST data too):</p>
<pre id="raw-php-8" style="display:none; width: 1px; height: 1px; overflow: hidden;">$url = 'http://othersite.com/someservice?' . http_build_query($_GET);
echo file_get_contents($url);</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-8" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-8'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$url</span> = <span style="color:#FF0000;">'http://othersite.com/someservice?'</span> . http_build_query<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <a href="http://www.php.net/file_get_contents"><span style="color:#000066;">file_get_contents</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$url</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
Remember, the sole purpose of the proxy is just so the client browser can load the data locally on the same domain so the same origin policy isn't violated.</p>
<h2>Drawbacks</h2>
<p>This method works well but has two obvious drawbacks. First is of course that you need a server-side script at all. Especially if you are providing a service, this raises the barrier for entry and makes it harder for "noobs" to use your widget -- it's not a simple "paste this code into your footer" instruction; you also need to explain how to install the proxy service.</p>
<p>The second drawback is that your own servers are making these requests which makes the whole process slower, but also eats up your resources -- both your processing power and even just simple bandwidth.</p>
<h1>Solution 2: JSONP</h1>
<p>The second solution is to use JSONP -- "JSON with Padding." This is a technique that lets you get around the same origin policy. In order to use JSONP, the service you are requesting needs to support it.</p>
<p>So in the last solution, the consumer (the user using the service) required an ajax_proxy.php or whatever server-side proxy script. In this solution, the provider (Digg, Amazon, Yahoo -- whatever) needs to support JSONP themselves. Thankfully, these days the idea is catching on and it's likely JSONP is an option. And if you're a service provider, then you'll want to build it in to your service for sure.</p>
<h2>How it works</h2>
<p>A normal JSON request is sent using XMLHttpRequest and the reply looks something like this:</p>
<pre id="raw-javascript-9" style="display:none; width: 1px; height: 1px; overflow: hidden;">{'uid': 23, 'username': 'Chroder', 'name': 'Christopher Nadeau'}</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-9" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-9'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-9">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'uid'</span>: <span style="color: #CC0000;color:#800000;">23</span>, <span style="color: #3366CC;">'username'</span>: <span style="color: #3366CC;">'Chroder'</span>, <span style="color: #3366CC;">'name'</span>: <span style="color: #3366CC;">'Christopher Nadeau'</span><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Now the truth is, JSONP isn't AJAX at all technically speaking because it does <em>not</em> use XMLHttpRequest. JSONP requests are made by dynamically inserting a &lt;script&gt; tag into the DOM. For example, you'd insert this:</p>
<pre id="raw-html-10" style="display:none; width: 1px; height: 1px; overflow: hidden;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://othersite.com/service?all=your&amp;params=as&amp;usual=gohere&quot;&gt;&lt;/script&gt;</pre>
<div class="igBar">
<div class="wrap"><span id="lhtml-10" style="float:right"><a href="#" onclick="javascript:showCodeTxt('html-10'); return false;">Plain Text</a></span><span class="langName">HTML:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="html-10">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/javascript"</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"http://othersite.com/service?all=your&amp;params=as&amp;usual=gohere"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Then that remote Javascript file is loaded and contains an actual Javascript function call. For example:</p>
<pre id="raw-javascript-11" style="display:none; width: 1px; height: 1px; overflow: hidden;">handleJsonReply({'uid': 23, 'username': 'Chroder', 'name': 'Christopher Nadeau'});</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-11" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-11'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-11">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">handleJsonReply<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'uid'</span>: <span style="color: #CC0000;color:#800000;">23</span>, <span style="color: #3366CC;">'username'</span>: <span style="color: #3366CC;">'Chroder'</span>, <span style="color: #3366CC;">'name'</span>: <span style="color: #3366CC;">'Christopher Nadeau'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>In other words, instead of reading AJAX data directly into a variable in Javascript, you define a callback function that is called when the data arrives, and the first parameter is the data itself as an object literal. If you were a provider implementing JSON on your service you might have something like:</p>
<pre id="raw-php-12" style="display:none; width: 1px; height: 1px; overflow: hidden;">// A service provider accepts a 'callback' parameter
// that it uses to wrap an AJAX reply

// Imagine we did some work here
$json = json_encode($mydata);

// Using JSONP
if ($_GET['callback']) {
	echo $_GET['callback'] . &quot;($json);&quot;; // somefunction({data here});

// Normal JSON
} else {
	echo $json;
}</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-12" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-12'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-12">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// A service provider accepts a 'callback' parameter</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// that it uses to wrap an AJAX reply</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Imagine we did some work here</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$json</span> = json_encode<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$mydata</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Using JSONP</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'callback'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'callback'</span><span style="color:#006600; font-weight:bold;">&#93;</span> . <span style="color:#FF0000;">"($json);"</span>; <span style="color:#FF9933; font-style:italic;">// somefunction({data here});</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Normal JSON</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$json</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The Javascript source code result of that request is nothing but a real executable function call with a Javascript object literal. It's as if you added the tag yourself and it called some functions -- the difference being that the JS is written by the producer on-the-fly and embeds the data you want right in the code. This is why it all works; it's not actually AJAX at all, it's just loading a remote Javascript file that happens to have some useful data in it.</p>
<p>Using a library like jQuery that supports JSONP, these details of inserting the special script tag and creating the special callback function are all taken care of automatically. Using a JS library, usually the only difference between JSONP and real AJAX is that you enable a 'jsonp' option.</p>
<h2>Drawbacks</h2>
<p>The first limitation to this method is that you have to rely on the provider to implement JSONP. The provider needs to actually support JSONP -- they need to wrap their JSON data with that callback function name.</p>
<p>Then the next limitation -- and this is a big one -- is that JSONP doesn't support POST requests. Since all data is passed in the query string as GET data, you are severely limited if your services require the passing of long data (for example, forum posts or comments or articles). But for the majority of consumer services that <em>fetch</em> more data than they push, this isn't such a big problem.</p>
<h2>JSONP with jQuery</h2>
<p>Step 1: Make sure the provider supports JSONP.<br />
Step 2: Set the <code>dataType</code> option to <code>jsonp</code>, and if the provider uses a different GET param other than 'callback', specify the <code>jsonp</code> option to that parameter name.</p>
<pre id="raw-javascript-13" style="display:none; width: 1px; height: 1px; overflow: hidden;">$.ajax({
    // ... Use the AJAX utility as you normally would
    dataType: 'jsonp',
    // ...
});</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-13" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-13'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-13">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$.<span style="color: #006600;">ajax</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// ... Use the AJAX utility as you normally would</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; dataType: <span style="color: #3366CC;">'jsonp'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// ...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>jQuery will generate a unique callback name for this request (something like json1268267816). Thus, the reply from a web service would be something like:</p>
<pre id="raw-javascript-14" style="display:none; width: 1px; height: 1px; overflow: hidden;">json1268267816({'uid': 23, 'username': 'Chroder', 'name': 'Christopher Nadeau'});</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-14" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-14'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-14">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">json1268267816<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'uid'</span>: <span style="color: #CC0000;color:#800000;">23</span>, <span style="color: #3366CC;">'username'</span>: <span style="color: #3366CC;">'Chroder'</span>, <span style="color: #3366CC;">'name'</span>: <span style="color: #3366CC;">'Christopher Nadeau'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
But jQuery handles it all seamlessly, so you as the developer just handle it like a normal AJAX request using the same jQuery success/failure/complete callback hooks.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2010/03/10/cross-domain-ajax/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Stream Wrappers</title>
		<link>http://devlog.info/2008/10/19/php-stream-wrappers/</link>
		<comments>http://devlog.info/2008/10/19/php-stream-wrappers/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 20:24:53 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Check It Out]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[stream-wrapper]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=57</guid>
		<description><![CDATA[If you talk to me regularly then you already know how much I love stream wrappers in PHP. The other day I was positively giddy with how easy it was to solve a particular problem by using stream wrappers. 
What are stream wrappers?
PHP 4.3.0 introduced the idea of streams. The PHP manual has a perfect [...]]]></description>
			<content:encoded><![CDATA[<p>If you talk to me regularly then you already know how much I love stream wrappers in PHP. The other day I was positively giddy with how easy it was to solve a particular problem by using stream wrappers. <span id="more-57"></span></p>
<h2>What are stream wrappers?</h2>
<p>PHP 4.3.0 introduced the idea of streams. The <a href="http://php.net/manual/en/intro.stream.php">PHP manual</a> has a perfect explanation:</p>
<blockquote><p>Streams were introduced with PHP 4.3.0 as a way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior. That is, it can be read from or written to in a linear fashion, and may be able to fseek() to an arbitrary locations within the stream. </p></blockquote>
<p>Streams are identified by a particular scheme in the URI. For example, with <code>http://someurl</code> the scheme is 'http' and when no scheme is provided like in <code>/file.php</code>, the scheme is defaulted to 'file' and is expected to be a file in the filesystem (thus <code>file:///file.php</code> would mean the same thing).</p>
<p><strong>Stream wrappers</strong> are bits of code that tell PHP how to handle certain schemes. The interesting thing is that you can create your own custom stream wrappers written in PHP with little effort.</p>
<p>For example, you may have done <code>file_get_contents('file.txt');</code> before. But say you wanted to have the same ease of use but with data from a different source. You could create a custom stream wrapper to get data from wherever you want and use all of the normal PHP functions as usual: <code>file_get_contents('mystream://whatever');</code>.</p>
<p>Almost <em>any</em> function that works on files will work with custom stream wrappers. That includes fopen, fwrite, fseek, is_readable, is_writable, stat, unlink and more. In essence you can create a virtual filesystem in your applications.</p>
<h2>Why create custom stream wrappers?</h2>
<p>So why in the world would you want to create custom stream wrappers? Basically it comes down to decoupling filesystem-coupled interfaces from the filesystem (or any <em>stream</em>, for that matter). That is, make software think it's talking to files when it's talking to your custom PHP class. It's all about abstraction!</p>
<p>You could also use stream wrappers to simplify an interface, though this is less common. If you are using stream wrappers to simplify an interface, you can just as easily create a normal <a href="http://en.wikipedia.org/wiki/Facade_pattern">facade</a> to simplify the interface and leave streams out of it.</p>
<h3>For example</h3>
<p>I wanted to use a newish PHP templating engine called <a href="http://dwoo.org/">Dwoo</a> for an upcoming project. The problem was it expects files everywhere. Template files were read from the filesystem and compiled to the filesystem, and caches were written to the filesystem. There was no easy way to change this.</p>
<p>For this particular project I need to read templates from a string (loaded from an arbitrary source decided at runtime), and I can't use the filesystem for caches, I need to use memcached. The reason being that the application will be run in a cluster. Using shared disks is not practical with this many machines, and disk IO would eventually become a bottleneck. True, we could use RAM disks, but it's a lot easier and portable to manage the data layer from our application instead of pushing it to the OS.</p>
<p>Using custom stream wrappers I created two new schemes for 'tpl://' (for reading) and 'tplc://' (for writing compiled templates). I was able to fill the requirements for the project while still using the awesome templating engine with little modification. The library still thinks it's reading and writing to files but behind the scenes it's reading from a cache of template sources and writing to memcached. Perfect!</p>
<h2>Creating custom stream wrappers</h2>
<p>To create your own custom stream wrappers you need to write a class that implements a set of methods. After you write the class, you can register it with <a href="http://php.net/manual/en/function.stream-wrapper-register.php">stream_wrapper_register</a>. Refer to that manual page for details on the methods you need.</p>
<p>The only problem I encountered was how to get my custom stream working with is_readable() and is_writable(). The trick is to implement the 'url_stat' method and set the 'mode' correctly. The mode is a number that represents what type of file the file is (file or directory etc) and the file permissions -- the usual chmod values. Here's a code snippet that accurately defines how the number should add up:</p>
<pre id="raw-php-16" style="display:none; width: 1px; height: 1px; overflow: hidden;">$dir = 040000;
$file = 0100000;

// Now you can add any permissions you want using the usual chmod octal notation:
$file += 0777; // A file that is readable
$dir += 0222; // A directory that is writable</pre>
<div class="igBar">
<div class="wrap"><span id="lphp-16" style="float:right"><a href="#" onclick="javascript:showCodeTxt('php-16'); return false;">Plain Text</a></span><span class="langName">PHP:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="php-16">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dir</span> = <span style="color:#CC66CC;color:#800000;">040000</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$file</span> = <span style="color:#CC66CC;color:#800000;">0100000</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// Now you can add any permissions you want using the usual chmod octal notation:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$file</span> += <span style="color:#CC66CC;color:#800000;">0777</span>; <span style="color:#FF9933; font-style:italic;">// A file that is readable</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$dir</span> += <span style="color:#CC66CC;color:#800000;">0222</span>; <span style="color:#FF9933; font-style:italic;">// A directory that is writable </span></div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2008/10/19/php-stream-wrappers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sounds, IE7 and Security Warnings</title>
		<link>http://devlog.info/2008/04/11/sounds-ie7-security-warnings/</link>
		<comments>http://devlog.info/2008/04/11/sounds-ie7-security-warnings/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 23:33:40 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Check It Out]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[ie7]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[sound manager]]></category>

		<guid isPermaLink="false">http://devlog.info/?p=30</guid>
		<description><![CDATA[Some of you may know that the last few weeks I've been writing an AJAX chat application that plugs in to DeskPRO. One of the problems we ran into is playing sound notifications. The problem is that IE7 likes to pop up a security warning when you use the usual &#60;embed&#62; code. This was unacceptable. [...]]]></description>
			<content:encoded><![CDATA[<p>Some of you may know that the last few weeks I've been writing an AJAX chat application that plugs in to DeskPRO. One of the problems we ran into is playing sound notifications. The problem is that IE7 likes to pop up a security warning when you use the usual &lt;embed&gt; code. This was unacceptable. So today I just want to briefly talk about how I solved the problem.<span id="more-30"></span></p>
<p>The solution that I came up with was to use Flash. Seeing as how over <a href="http://www.adobe.com/products/player_census/flashplayer/">98% of all users</a> have Flash installed, it was the perfect solution.</p>
<p>I ended up finding an open source project called <a href="http://www.schillmania.com/projects/soundmanager2/">Sound Manager</a>. You just embed the Flash movie on your page somewhere, and talk to it with plain old Javascript. You don't need Flash installed, you don't need to create your own movie -- all you do is use the API provided by Sound Manager.</p>
<p>For the curious ones amongst you, the API is really simple (taken directly from the linked page above):</p>
<pre id="raw-javascript-18" style="display:none; width: 1px; height: 1px; overflow: hidden;">soundManager.createSound('myNewSound','/path/to/some.mp3');

soundManager.play('myNewSound');
soundManager.setVolume('myNewSound',50);
soundManager.setPan('myNewSound',-100);</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-18" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-18'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-18">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">soundManager.<span style="color: #006600;">createSound</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'myNewSound'</span>,<span style="color: #3366CC;">'/path/to/some.mp3'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">soundManager.<span style="color: #006600;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'myNewSound'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">soundManager.<span style="color: #006600;">setVolume</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'myNewSound'</span>,<span style="color: #CC0000;color:#800000;">50</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">soundManager.<span style="color: #006600;">setPan</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'myNewSound'</span>,-<span style="color: #CC0000;color:#800000;">100</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The <em>only</em> problem I had with using Sound Manager is that it requires MP3's, and all of our sounds were WAV's. No biggie. Converting the files to MP3's was simple, and the resulting filesize a bit smaller anyway.</p>
<h3>Even more accessible?</h3>
<p>98% of internet users have Flash. That's a pretty good percentage. But I still wanted to make sure those rare 2% of users didn't get some "install this plugin" popup.</p>
<p>Basically what I ended up doing was using a Flash sniffer (some JS to determine if Flash is installed, and which version) to see if the user can use the Sound Manager. If they could use it, then great -- we're all set to go. If they couldn't, then the app reverts back to using good ol'd &lt;embed&gt;'s (except for IE7; no sound is better then an annoying security popup!).</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2008/04/11/sounds-ie7-security-warnings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto-DST Detection</title>
		<link>http://devlog.info/2007/11/01/auto-dst-detection-javascript-php/</link>
		<comments>http://devlog.info/2007/11/01/auto-dst-detection-javascript-php/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 14:40:39 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[daylight savings]]></category>
		<category><![CDATA[dst]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://devlog.info/2007/11/01/auto-dst-detection/</guid>
		<description><![CDATA[Online apps usually have a way to set your timezone so that all times are local to you. One common feature many end-users expect is some mechanism that automatically detects when DST is on/off and changes things accordingly.
The easiest way I've found to do this is with Javascript. It's dreadfully simple code. Here's a sample [...]]]></description>
			<content:encoded><![CDATA[<p>Online apps usually have a way to set your timezone so that all times are local to you. One common feature many end-users expect is some mechanism that automatically detects when DST is on/off and changes things accordingly.<span id="more-24"></span></p>
<p>The easiest way I've found to do this is with Javascript. It's dreadfully simple code. Here's a sample of what I usually put at the footer of my pages that uses Javascript and PHP:</p>
<pre id="raw-html-20" style="display:none; width: 1px; height: 1px; overflow: hidden;">
&lt;!-- All of your HTML here --&gt;

&lt;?php if($do_dst_check): ?&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	var timezone = &lt;?php echo $timezone + $dst_offset; ?&gt;;
	var utc = new Date().getTimezoneOffset() / 60;
	if (Math.abs(timezone + utc) == 1) {
		$.ajax({
			type: 'GET',
			url: 'ajax_update_dst.php',
			success: function() { location.reload(); }
		});
	}
	&lt;/script&gt;
&lt;?php endif; ?&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
<div class="igBar">
<div class="wrap"><span id="lhtml-20" style="float:right"><a href="#" onclick="javascript:showCodeTxt('html-20'); return false;">Plain Text</a></span><span class="langName">HTML:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="html-20">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- All of your HTML here --&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;</span></a>?php if<span style="color: #66cc66;">&#40;</span>$do_dst_check<span style="color: #66cc66;">&#41;</span>: ?<span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/javascript"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; var timezone = <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;</span></a>?php echo $timezone + $dst_offset; ?<span style="color: #000000; font-weight: bold;">&gt;</span></a></span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; var utc = new Date().getTimezoneOffset() / 60;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; if (Math.abs(timezone + utc) == 1) {</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'GET',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'ajax_update_dst.php',</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function() { location.reload(); }</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; });</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; }</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;</span></a>?php endif; ?<span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<h2>How It Works</h2>
<p>First you get the time offset that the user has entered into your application, the one you have on record. You need to get the timezone selection and then the current DST offset. For example, if DST is "on" then that just means you +1 to the users selected timezone.</p>
<p>Javascript has something your server-side scripts might not: it knows about the users locale. So using this fact, we can get the users current timezone offset. This is the "real" offset, the one we know is correct, regardless of what they inputted into your application.</p>
<p>Finally, you simply add the offset your application has on record with the users "real" offset. If the result is exactly -1 or 1 (here I used Math.abs, so -1 becomes 1), that means the information you have on record is incorrect, and the DST status needs to be toggled.</p>
<p>In this example I use a <a href="http://docs.jquery.com/Ajax">jQuery AJAX</a> call to request a page that would toggle the users DST setting.</p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2007/11/01/auto-dst-detection-javascript-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Objects and Member Visibility</title>
		<link>http://devlog.info/2007/10/20/javascript-objects-and-member-visibility/</link>
		<comments>http://devlog.info/2007/10/20/javascript-objects-and-member-visibility/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 21:10:16 +0000</pubDate>
		<dc:creator>Christopher</dc:creator>
				<category><![CDATA[Application Design]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[visibility]]></category>

		<guid isPermaLink="false">http://devlog.info/2007/10/20/javascript-objects-and-member-visibility/</guid>
		<description><![CDATA[So many web developers know just a little about Javascript. Enough to use a library like jQuery somewhat effectively, but not enough to understand how to create properly structured applications. I'm not going to get into how objects work in Javascript, or how to use them, I'll leave that for another day. I just want [...]]]></description>
			<content:encoded><![CDATA[<p>So many web developers know just a little about Javascript. Enough to use a library like <a href="http://jquery.com/">jQuery</a> somewhat effectively, but not enough to understand how to create properly structured applications. I'm not going to get into how objects work in Javascript, or how to use them, I'll leave that for another day. I just want to touch on the topic of <em>member visibility</em> (that is, private/protected methods and properties).<span id="more-23"></span></p>
<p>There is no formal way to actually define member visibility in Javascript (no "public" and "private" keywords like you might see in Java). Instead we just use the language features in such a way that it creates the same public/private situation as we'd expect in other languages (or nearly the same).</p>
<h2>Public</h2>
<p>A member is public if "the world" can access it. For methods this means anyone can call on it, and for properties this means anyone can use its value or change it. If you are creating object-oriented programs, having nothing but public members breaks a lot of the ideology. In many languages, like PHP before version 5, programmers used conventions to denote members were private and not to be used. Sometimes this works, but true encapsulation is much better. </p>
<p>There are two ways you create public members:</p>
<h3>Constructor</h3>
<pre id="raw-javascript-28" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {
    this.name = 'Christopher';
}

var me = new Person();
alert(me.name);</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-28" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-28'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-28">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> me = <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>me.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
Using 'this' binds the variable to the object, so anyone can use it.</p>
<h3>Prototype</h3>
<pre id="raw-javascript-29" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {

}

Person.prototype.name = 'Christopher';

var me = new Person();
alert(me.name);</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-29" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-29'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-29">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Person.<span style="color: #006600;">prototype</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> me = <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>me.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>Adding a method or property to the prototype also makes it public.</p>
<h2>Private</h2>
<p>A member is private if only the object itself can access it.</p>
<p>To create private members, you define them within the constructor:</p>
<pre id="raw-javascript-30" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {
    var first_name = 'Christopher';
    var last_name = 'Nadeau';

    function fullname() {
        return first_name + ' ' + last_name;
    }
}

var me = new Person();
alert(me.name); // can't do it!</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-30" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-30'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-30">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> first_name = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> last_name = <span style="color: #3366CC;">'Nadeau'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> fullname<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> first_name + <span style="color: #3366CC;">' '</span> + last_name;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> me = <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>me.<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// can't do it! </span></div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p>
In this example there are two private properties for first and last name, and one private method to return a full name.</p>
<h3>Some Issues...</h3>
<p>There are a couple of issues you have to be aware of when using private members.</p>
<p>Only methods defined in the constructor can use private members. That is to say, we <em>cannot</em> do something like this:</p>
<pre id="raw-javascript-31" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {
    var first_name = 'Christopher';
    var last_name = 'Nadeau';

    function fullname() {
        return first_name + ' ' + last_name;
    }
}

Person.prototype.alertName = function() {
    // Can't do this. 'this.first_name' does not actually exist!
    alert(this.first_name);
}</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-31" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-31'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-31">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> first_name = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> last_name = <span style="color: #3366CC;">'Nadeau'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> fullname<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> first_name + <span style="color: #3366CC;">' '</span> + last_name;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Person.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">alertName</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></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Can't do this. 'this.first_name' does not actually exist!</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">first_name</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>The scope of the private members are in the constructor only. To get around this problem, you must use what is called <em>privileged methods</em>.</p>
<h2>Privileged</h2>
<p>As stated before, only methods defined within the constructor have access to private members. Given that many objects are built around using 'prototype', this is problematic. The solution is to create privileged methods.</p>
<p>A privileged method is a method defined in the constructor but assigned to a public variable. Since it is defined within the scope of any other private members, it has access to them. Here's an example:</p>
<pre id="raw-javascript-32" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {
    var first_name = 'Christopher';
    var last_name = 'Nadeau';

    function fullname() {
        return first_name + ' ' + last_name;
    }

    this.alertName = function() {
        alert(first_name);
    }
}</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-32" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-32'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-32">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> first_name = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> last_name = <span style="color: #3366CC;">'Nadeau'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> fullname<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> first_name + <span style="color: #3366CC;">' '</span> + last_name;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">alertName</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></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>first_name<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>You can guess that you will be able to retrieve the 'alertName' method or reset it. You might think that it would be possible to redefine the method to take control of some other private member. Not so! The method is a closure and retains the scope in which it was defined. Since it was defined in the scope of the other private members, it has access to them. There is no way to recreate this scope, so even reassigning a completely new function will not allow you to expose any private members.</p>
<h3>The 'this' reference</h3>
<p>Another thing to be aware of is that functions defined within the constructor have "this" pointing to themselves, and not the object itself. Thus if you need to call other public methods, you cannot. For example, let's make the fullname() method privileged and modify alertName() to call it:</p>
<pre id="raw-javascript-33" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {
    var first_name = 'Christopher';
    var last_name = 'Nadeau';

    // This is now a privileged method
    this.fullname = function() {
        return first_name + ' ' + last_name;
    }

    this.alertName = function() {
        alert(this.fullname()); // wont work
    }
}</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-33" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-33'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-33">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> first_name = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> last_name = <span style="color: #3366CC;">'Nadeau'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// This is now a privileged method</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">fullname</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></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> first_name + <span style="color: #3366CC;">' '</span> + last_name;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">alertName</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></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">fullname</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// wont work</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
<p>To get around this problem, Javascript programmers assign another private property with the value of 'this' inside the constructor, and use that variable as a reference to the object. The two most common names are 'self' and 'that'; I prefer to use 'self'. Here is the final object, working as expected:</p>
<pre id="raw-javascript-34" style="display:none; width: 1px; height: 1px; overflow: hidden;">function Person() {
    var first_name = 'Christopher';
    var last_name = 'Nadeau';

    // Create a reference to this object so closures
    // can access it.
    var self = this;

    this.fullname = function() {
        return first_name + ' ' + last_name;
    }

    this.alertName = function() {
        alert(self.fullname()); // Using self to reference the object
    }
}</pre>
<div class="igBar">
<div class="wrap"><span id="ljavascript-34" style="float:right"><a href="#" onclick="javascript:showCodeTxt('javascript-34'); return false;">Plain Text</a></span><span class="langName">JAVASCRIPT:</span>
</div>
</div>
<div class="syntax_hilite">
<div class="wrap">
<div id="javascript-34">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> first_name = <span style="color: #3366CC;">'Christopher'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> last_name = <span style="color: #3366CC;">'Nadeau'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Create a reference to this object so closures</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// can access it.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> self = <span style="color: #000066; font-weight: bold;">this</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">fullname</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></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> first_name + <span style="color: #3366CC;">' '</span> + last_name;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">alertName</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></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>self.<span style="color: #006600;">fullname</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// Using self to reference the object</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; font-weight:bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://devlog.info/2007/10/20/javascript-objects-and-member-visibility/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
