<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Hidden UI</title>
	<atom:link href="http://jamescryer.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamescryer.wordpress.com</link>
	<description>{ &#039;A blog by James Cryer&#039; : &#039;*.js, *.css, *.*&#039; }</description>
	<lastBuildDate>Mon, 04 Mar 2013 22:04:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jamescryer.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Hidden UI</title>
		<link>http://jamescryer.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jamescryer.wordpress.com/osd.xml" title="Hidden UI" />
	<atom:link rel='hub' href='http://jamescryer.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Contracts for automated UI testing</title>
		<link>http://jamescryer.wordpress.com/2013/02/12/ui-test-contacts/</link>
		<comments>http://jamescryer.wordpress.com/2013/02/12/ui-test-contacts/#comments</comments>
		<pubDate>Tue, 12 Feb 2013 23:44:32 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=542</guid>
		<description><![CDATA[I&#8217;ve seen people write tests that assert that a button has a &#8216;disabled&#8217; CSS classname to test if a button is disabled. I have then seen those same tests break because someone changed the styles so that &#8216;btn-disabled&#8217; is now the classname of choice for styling that state, but of course nothing actually broke, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=542&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve seen people write tests that assert that a button has a &#8216;disabled&#8217; CSS classname to test if a button is disabled. I have then seen those same tests break because someone changed the styles so that &#8216;btn-disabled&#8217; is now the classname of choice for styling that state, but of course nothing actually broke, the test failure was a false negative. At the same time the old styles for the &#8216;disabled&#8217; class have been removed, but some buttons have been left with the old classname &#8211; tests still pass but it is (to the user) broken, a false positive. The example test below shows this kind of assert.</p>
<p><pre class="brush: plain;">
assert(page.myButton).hasClassName('.disabled', 'Should be disabled');
</pre><span id="more-542"></span></p>
<p>The above example is simply the result of a false assumption that the CSS classname represents a contract of state for that button. CSS classnames should never be used as a contract between UI and test, they&#8217;re not and should not be coupled to functionality because (more often than not) visual design is changed more than functionality. The only way to test if a button is disabled, is to click it and assert that nothing happened. This of course is just a functional test of the UI, it won&#8217;t tell you if the button is hidden or the wrong colour.</p>
<p><pre class="brush: plain;">
click(page.myButton);
assert(page.myReceipt).doesnot.containText('Action complete', 'Should not have completed action');
</pre></p>
<p>In the snippet above the page elements are probably defined using a CSS or XPATH selector, this implies that there is still a contract between the tests and the UI. Let&#8217;s say page.myButton is defined with the following CSS selector <code>'div.popupbox .footer &gt; button'</code>. By using this selector you are assuming that the button in question will always be inside a div element with a classname of &#8216;popupbox&#8217;, the button will always live inside a footer, the button will always be &#8230; a button, not a link, you sure about that? <strong>Coupling your tests to markup and styling will lead to incredibly fragile tests</strong>, especially in an environment where UI design is constantly iterated upon.</p>
<p>To overcome this fragility, rather than assume a contact, define a contract. Creating a consistant vocabulary for element hooks for testing is quite easy. At Huddle we&#8217;ve defined a contract using data attributes in the markup, the CSS3 selector for the disabled button could look like this <code>'[data-automation=submit-comment-button]'</code>, note that the intent of the button is used for naming, not the style. Decorating code like this makes markup a little messier but gives an immediate prompt to a future developer that this page is under test while also giving them the freedom to change styling without having to update and fix tests. Of course you can make up a contract that bests suits you and your team, perhaps web-aria attributes might be better?</p>
<p>But what about contracts for visual testing, couldn&#8217;t we just agree that <code>'[data-automation=disabled]'</code> means that the button is visually disabled? No, it just doesn&#8217;t work. You&#8217;ve given some protection to the CSS developer that&#8217;s re-skinning the UI but you have no guarantee that the styling won&#8217;t have broke, nor that clicking the button doesn&#8217;t submit the form. You have to simulate the user, it&#8217;s the only reliably way to test UI.</p>
<p>In a future post I will discuss how visual regression testing is possible.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/542/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=542&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2013/02/12/ui-test-contacts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>Isolated UI testing with PhantomJS</title>
		<link>http://jamescryer.wordpress.com/2012/07/31/uitesting/</link>
		<comments>http://jamescryer.wordpress.com/2012/07/31/uitesting/#comments</comments>
		<pubDate>Tue, 31 Jul 2012 14:44:04 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CasperJS]]></category>
		<category><![CDATA[Isolated UI testing]]></category>
		<category><![CDATA[jstd]]></category>
		<category><![CDATA[PhantomJs]]></category>
		<category><![CDATA[Sinon.JS]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=526</guid>
		<description><![CDATA[If you&#8217;re working on a JavaScript web application that uses APIs to separate the concerns of UI and domain model then you can test your UI as an isolated concern.  Mock the Ajax responses from your API, write functional UI tests, and make assertions on outgoing requests. I&#8217;ve posted before no how to use JSTD for unit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=526&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>If you&#8217;re working on a JavaScript web application that uses APIs to separate the concerns of UI and domain model then you can test your UI as an isolated concern.  Mock the Ajax responses from your API, write functional UI tests, and make assertions on outgoing requests.</strong></p>
<p><span id="more-526"></span></p>
<p>I&#8217;ve posted before no how to use <a href="http://jamescryer.wordpress.com/2011/03/22/javascript-bdd-with-jasmine-and-jstestdriver/" target="_blank">JSTD for unit testing JavaScript</a>.  Unit level test coverage encourages better design and supports refactoring through fast  feedback.  It really does make a difference to code confidence and although I personally do minimal TDD, writing tests as you go can help massively in understanding problems and fleshing out solutions.</p>
<p>Awesome, great for logical code but&#8230; I have found that unit tests for behavioural UI code provide little in the way of qualitative feedback and TDD can actually hinder UI problem solving by abstracting the developer away from the real problem they&#8217;re trying to solve, the UX problem.</p>
<p>So given the lack of total coverage with unit tests its understandable that we&#8217;d want to build functional UI tests as well.  The most popular tool for that would be <a href="http://seleniumhq.org/">Selenium</a>, a tool for automating web browsers.  Selenium is useful for integration testing, that is, end-to-end, make sure the website/webapp works completely in its entirety. But Selenium is slow, super slow; especially with web applications where you need to set up real data for each test.  Fine for integrated testing, not fine for functional UI testing. Integration and UI testing are different things and should be done separately.</p>
<p>There is of course a new kid in town. <a href="http://phantomjs.org/" target="_blank">PhantomJS </a>attracted me for two reasons.  The first being that it&#8217;s a headless browser, so runs from the command line and is really quick. The second is that it lets us easily interact with web pages using JavaScript (crazy idea, who came up with that?).</p>
<p>PhantomJS by itself is pretty raw, I decided to use it with the <a href="http://casperjs.org/" target="_blank">CasperJS </a>library to help build up navigation scenarios. CasperJS gives us all the cool test assertion stuff as well as handy methods for finding elements and raising DOM events.  Now here&#8217;s the clever bit. <a href="http://sinonjs.org/" target="_blank">Sinon.JS</a> provides an XHR mocking interface, it can block all XHR requests and let you respond to these requests with fake data.  By injecting Sinon.JS on to the page at the beginning of a test suite not only can we isolate the UI from the back-end API, but we can drive the UI with fake data and even make assertions on outgoing requests.</p>
<p>Below I&#8217;ve drafted a small example of what I mean (It doesn&#8217;t work, but represents a simpler implementation of what I have already built). CasperJSs page.initialized event is very useful for setting up XHR mocks early on, before any window.onload API calls are made.</p>
<p><pre class="brush: plain;">
casper.
start(rootUrl).
addListener('page.initialized', function(){
  casper.page.injectJs( libs+'/sinon.js' );
  casper.evaluate(funciton(){
    window._xhr = sinon.useFakeXMLHttpRequest();
    window._requests = {};

    window._xhr.onCreate = function (xhr) {
      var headers, body;

      if(request.method === 'GET'){

        headers = { &quot;Content-Type&quot;: &quot;application/json&quot; };
        body = '[{ &quot;id&quot;: 12, &quot;comment&quot;: &quot;Faking it is fun!&quot; }]';

        // fake some data to make the UI do something
        xhr.respond(200, headers, body); 
      } else {

        window._requests[xhr.url] = true;
      }
    };
  });
}).
thenOpen(pageUrl, function(){
  casper.click('#my-button'); // trigger some ajax
}).
then(function(){
  var request = casper.evaluate(function(){
    return window._requests;
  });

  // test the HTTP interface
  casper.test.assert( request[posturi] === true, 'Should have posted a request'); 

  // test the User interface
  casper.test.assertTextExists('Faking it is fun!', 'Should have the fake comment in the UI');

});
// ...
</pre></p>
<p>Setting up Fake Ajax responses this way is a little clumsy, the solution I built at work abstracted this code away so that in the tests we only call a fakeResponse method and a assertRequest method.  PhantomJS in general is very hackable, really easy to extend.</p>
<p>I&#8217;ve evangelised this approach to testing in my workspace and we have now fully adopted automated, isolated UI testing using these technologies. An unexpected side effect of faster feedback from automated UI testing is that we write fewer unit tests. If the feedback time is quick, may as well push as much of the testing to the front as possible. We still write unit tests for complicated logic driven JS components but get the bulk of coverage from the UI tests.  There&#8217;s a balance obviously but this inevitably means less test duplication but better coverage. Oh and of course we still have Web integration tests (using Selenium) but write far fewer, tests aimed purely at end-to-end connectivity not functional UI.</p>
<p>So we now have fast, smart, flexible test coverage over our JavaScript application.  Writing UI tests is no longer a burden. To quote one of my colleagues: &#8220;UI testing is fun again!&#8221;.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/526/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/526/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=526&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2012/07/31/uitesting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>Some notes on Grumble.js, CoDesign.js &amp; Artify.js</title>
		<link>http://jamescryer.wordpress.com/2012/04/02/project-notes/</link>
		<comments>http://jamescryer.wordpress.com/2012/04/02/project-notes/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 21:44:23 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[opensource]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=503</guid>
		<description><![CDATA[Grumble.js, CoDesign.js &#38; Artify.js are my three most successful projects on Github. They are all successful in different ways but one common success was simply that I learnt something from each of them. Grumble.js Grumble.js came from a project at work (Huddle). I was given a fairly unreasonable specification: round speech-bubble like tool-tips that can [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=503&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://jamescryer.github.com/grumble.js/" target="_blank">Grumble.js</a>, <a href="http://jamescryer.github.com/coDesign.js/" target="_blank">CoDesign.js</a> &amp; <a href="http://jamescryer.github.com/Artify.js/" target="_blank">Artify.js</a> are my three most successful projects on Github. They are all successful in different ways but one common success was simply that I learnt something from each of them.</p>
<p><span id="more-503"></span></p>
<h2>Grumble.js</h2>
<p><a href="http://jamescryer.github.com/grumble.js/" target="_blank">Grumble.js</a> came from a project at work (<a href="http://www.huddle.com/" target="_blank">Huddle</a>). I was given a fairly unreasonable specification: round speech-bubble like tool-tips that can be positioned at many angles, appear in multiple colours, be animated in sequence, contain translatable/localised text and work in every browser from IE6 to the latest Firefox.</p>
<p><strong>T</strong><strong>he original project took me alone, about fifteen days</strong>. After I started I quickly decided not to use SVG/VML in favour of using background images, CSS3 and IE filters. I wanted to build it as a jQuery plugin because it was a good pattern, and patterns are better than no patterns. <strong>I learnt a huge amount about CSS3 and calculating axis</strong>, and was amazed by what could be done in IE6 with filters.</p>
<p>Originally I had no intention to open-source Grumble.js. But I thought I had built a solid piece of code that other people could use<strong></strong>. When Huddle introduced &#8216;Tuesday time&#8217;/opensource day I decided to push the code to Github with a demo page, about a week later I posted links to it from Hacker News and Digg.</p>
<p>Purely out of interest I hooked up Google Analytics. Grumble.js was hugely popular with lots of coverage from blogs from around the world.  <strong>It&#8217;s documentation page has received over fifty thousand visits.</strong></p>
<h2>CoDesign.js</h2>
<p>There was so much noise and excitement about the real-time web when NodeJS came out that I had to have a play.  So one lazy Sunday I used NodeJS and Socket.IO to build a real-time chat application. Was nothing fancy but it was easy enough to make me think that I could build a real-time drawing application.</p>
<p><a href="http://jamescryer.github.com/coDesign.js/" target="_blank">CoDesign.js</a> was the client-side implementation of this drawing application, and it wasn&#8217;t simple, and still isn&#8217;t finished.</p>
<p>Simple real-time drawing is quite easy, you simply push colour and x  y coordinates to the server to be broadcasted to the other clients; but complex image effects are far too large to push down the wire pixel by pixel. I learnt that<strong> the only way to make real-time drawing plausible is through the encapsulation of commands</strong> i.e. an instruction to use the &#8216;spray&#8217; brush at x &amp; y.  The processing has to be done on every client rather than sending a dataUrl encoded image for example.</p>
<p>Rather than building a functional set of features I was more interested in building fancy colour schemes and weird brushes. <strong> I wanted to do what <a href="http://mrdoob.com" target="_blank">Mr Doob</a> had done with <a href="http://mrdoob.com/projects/harmony/" target="_blank">Harmony</a></strong>, but in a way that would allow real-time collaboration on the creation of images. I&#8217;ve learnt a lot about working with HTML5 Canvas, JS performance and real-time web apps from this project. Oh and there&#8217;s a lot of maths in there, at least now I know about Sine waves!</p>
<p>Other than for the things I learnt, CoDesign.js was successful in that It&#8217;s awesome, and <a href="http://jamescryer.no.de/codesign" target="_blank">exciting to use</a>. I just wish I had the momentum and ideas to make it truly notable.</p>
<h2>Artify.js</h2>
<p>The latest project, and as of writing this blog, unpublicised. <a href="http://jamescryer.github.com/Artify.js/" target="_blank">Artify.js</a> was a great little project, It took about a day to build.  I learnt about the <strong>HTML file API, drag/drop events and image analysis</strong>.  I also decided to play with <a href="http://twitter.github.com/bootstrap/">Twitter&#8217;s Bootstrap</a> for the first time to help build the documentation page.  It&#8217;s a nice little plugin that converts images into art with CSS3, and because the output of the plugin is just a bunch on DOM elements it&#8217;s really easy to style and &#8216;artify&#8217; as you want.</p>
<p>All in all I think for the purpose of learning Artify.js has been my most successful project to date.  Unlike the other two, <strong>it&#8217;s not that useful but I learnt a load in just one day</strong>. I&#8217;ll certainly be looking to do more smaller projects from now on, so long as I get to learn something.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/503/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/503/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=503&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2012/04/02/project-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>Asynchronous UI: When to lie</title>
		<link>http://jamescryer.wordpress.com/2012/03/18/asynchronous-ui/</link>
		<comments>http://jamescryer.wordpress.com/2012/03/18/asynchronous-ui/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 13:32:36 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[UX]]></category>
		<category><![CDATA[Asynchronous UI]]></category>
		<category><![CDATA[MV*]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=456</guid>
		<description><![CDATA[Let&#8217;s work on the premise that we are comfortable building user interfaces that lie. We won&#8217;t lie when something was successful, but we will lie when there is a temporary internet connection failure or an internal server error. The user might be annoyed when they find out (if they find out), they might lose trust [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=456&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s work on the premise that we are comfortable building user interfaces that lie. We won&#8217;t lie when something was successful,  but we will lie when there is a temporary internet connection failure or an internal server error. The user might be annoyed when they find out (if they find out), they might lose trust in our application; or, they might try again, and it&#8217;ll probably be successful this time.</p>
<p><span id="more-456"></span></p>
<p>Asynchronous non blocking UIs do not wait, they lie. If there was no such thing as HTTP latency and un-performant databases then there would be no need to lie. Waiting might be the safest thing to do but it will slow down the user, distract them, frustrate them; and for what? Most of the time there is no need, we have all the data we need to show them what they expect to see.</p>
<p>Lying only works if it&#8217;s rare.  No one likes a liar but everyone lies every now and again, right? We accept this in our relationships with people, and I think it is also true to say that we accept this in our relationships with web applications.  No one likes big lies, but small lies are more acceptable.</p>
<p>So, we need a reliable back-end system so we don&#8217;t have to lie too much. And then we need to break down user tasks so that if we do lie it&#8217;s not about the loss of their freshly typed fifty page document.</p>
<p>Of course, to lie we must first know what the user expects to see. With regard to single page web apps: the client JavaScript application must be able to build new HTML; and as we&#8217;re building views on the client we&#8217;ll probably want our data as JSON from the server. Let&#8217;s assume we&#8217;re using an MV* framework like KnockoutJS or BackboneJS. JSON is retrieved from the server, the controller updates the model, the view updates with data from the model.  When the UI lies, the model is populated directly from data collected from user input.  If some data isn&#8217;t available then the UI can either fudge the data (maybe the user won&#8217;t notice?) or more honestly, extract data from a pre-made cache.</p>
<p>The lie must be convincing.  Start getting data as soon as you think you&#8217;ll need it, behind the scenes.  Create a cache that contains everything you&#8217;ll need. Use persistent browser cache when possible, not all data changes frequently. Inversely, request updates from the server API regularly for more changeable data &#8211; and don&#8217;t let the user know you&#8217;re doing it.</p>
<p>You can&#8217;t always lie.  Needless to say if the user wants to upload a file off-line, they can&#8217;t, they know they can&#8217;t so we shouldn&#8217;t pretend they have.  But a non-blocking UI shouldn&#8217;t stop them from queuing an action for when they do become online.  Feedback is really important, with a file upload for example we could just tell them that the upload will happen later.  File upload is actually an interesting case because the multi-part request is probably going to be long running and comparatively fragile. It&#8217;s a good idea in this case to let them know that something is happening in the background and listen for a window unload event to let them know their request will not be completed.</p>
<p>The interesting thing about building UIs that lie is that it enforces an architecture where the client has to fend for itself without server support, i.e. the client can work off-line. But also, by separating the concerns of API request handling and view state management it becomes easy to extend the data access layer to use other transport mediums such as web sockets or JSONP. Better composition also means easier automated testing.</p>
<p>Although there are architectural benefits to building asynchronous UIs, the main driver for such an approach is UX.  The user really doesn&#8217;t care what the software has to do, they just want to complete their task and move on to the next one. Asynchronous UIs help support user workflows, but to make asynchronous UIs work we have to lie and I think this is wholly acceptable and something that we as users have already begun to accept.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/456/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=456&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2012/03/18/asynchronous-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript Scope: with()</title>
		<link>http://jamescryer.wordpress.com/2012/02/14/javascript-scope-with/</link>
		<comments>http://jamescryer.wordpress.com/2012/02/14/javascript-scope-with/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 12:14:45 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Advanced JavaScript]]></category>
		<category><![CDATA[javascript scope]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=406</guid>
		<description><![CDATA[JavaScript unlike other C like languages does not have block scope, variable definitions are not exclusive to code blocks inside any pair of curely brackets unless the brackets belong to a function declaration. Well almost. The with statement creates a new scope based on the properties of an object. The above example shows how we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=406&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>JavaScript unlike other C like languages does not have block scope, variable definitions are not exclusive to code blocks inside any pair of curely brackets unless the brackets belong to a function declaration.  Well almost. The with statement creates a new scope based on the properties of an object.</p>
<p><span id="more-406"></span></p>
<p><pre class="brush: plain;">
var obj = {
  foo: 'bar'
};

with( obj ){
  console.log(foo); // bar
  foo = 'something';
}

console.log(obj.foo) // something
</pre></p>
<p>The above example shows how we can access and assign properties on the object as if they were variables in the local scope.</p>
<p>As you can see in the below example, the scope chain is the same as if it were a function closure.</p>
<p><pre class="brush: plain;">
var foo = 1,
    bar = 2,
    obj = {
      foo: 3
    };

with( obj ){
  console.log(foo); // 3
  console.log(bar); // 2
}

console.log(foo); // 1
</pre></p>
<p>Within the scope of a with statement the &#8216;this&#8217; keyword will still refer to the parent object.</p>
<p><pre class="brush: plain;">
var obj = {
  foo: 'bar'
};

with( obj ){
  this.foo = 'boofar';
}

console.log(obj.foo) // bar
console.log(this.foo) // boofar
</pre></p>
<p>So why might you use the with statement?  Generally you wouldn&#8217;t, it&#8217;s a little confusing to read, and doesn&#8217;t scale well with larger blocks of code. Douglas Crockford considers <a href="http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/" target="_blank">with to be harmful</a>, though stackoverflow does offer a <a href="http://stackoverflow.com/questions/61552/are-there-legitimate-uses-for-javascripts-with-statement" target="_blank">legitimate use for with</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/406/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=406&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2012/02/14/javascript-scope-with/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript Scope: Hoisting</title>
		<link>http://jamescryer.wordpress.com/2012/02/13/javascript-scope-hoisting/</link>
		<comments>http://jamescryer.wordpress.com/2012/02/13/javascript-scope-hoisting/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 22:34:30 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Advanced JavaScript]]></category>
		<category><![CDATA[hoisting]]></category>
		<category><![CDATA[javascript scope]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=404</guid>
		<description><![CDATA[Hoisting is the effect of the JavaScript runtime alotting variable space for an entire execution context regardless of when the &#8216;var&#8217; statement is used. Essentially, each &#8216;var&#8217; statement gets moved to the top of the function but the assignment is left where it is. In the example below the variable &#8216;bart&#8217; is defined in both [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=404&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Hoisting is the effect of the JavaScript runtime alotting variable space for an entire execution context regardless of when the &#8216;var&#8217; statement is used. Essentially, each &#8216;var&#8217; statement gets moved to the top of the function but the assignment is left where it is.</p>
<p><span id="more-404"></span></p>
<p>In the example below the variable &#8216;bart&#8217; is defined in both the outer scope and the inner scope of the method &#8216;say&#8217;.  </p>
<p><pre class="brush: plain;">
var bart = 'cowabunga';

var say = function(){
  if (bart == 'cowabunga') return bart;
  var bart = 'ay caramba';
  return bart;
}

console.log(say()); // 'ay caramba'
}
</pre></p>
<p>Because the variable &#8216;bart&#8217; gets hoisted, when the &#8216;say&#8217; method is executed &#8216;bart&#8217; is undefined. &#8216;ay caramba&#8217; will be logged.  We can fix this code by removing the &#8216;var&#8217; statement in front of the second &#8216;bart&#8217; declaration.</p>
<p>Hoisting also affects function declarations.  As you might expect the following example fails, &#8216;bartSay&#8217; is not defined.</p>
<p><pre class="brush: plain;">
console.log(bartSay());

var bartSay = function(){
  return 'cowabunga';
}
</pre></p>
<p>However if bartSay was defined in another way hoisting will come into play. Below I have defined &#8216;bartSay&#8217; as a &#8216;named&#8217; function rather than assigning an anonymous function to a variable called &#8216;bartSay&#8217;. Because there is no assignment the function gets hoisted intact.</p>
<p><pre class="brush: plain;">
(function(){
  console.log(bartSay());

  function bartSay(){
    return 'cowabunga';
  }
})();
</pre></p>
<p>The code below however, will fail. Despite sharing the same global object, each script block creates a new execution context. <a title="Stackoverflow: JavaScript programs" href="http://stackoverflow.com/questions/3735406/how-many-javascript-programs-are-executed-for-a-single-web-page-in-the-browser">More here.</a></p>
<p><pre class="brush: plain;">
&lt;script type=&quot;text/javascript&quot;&gt;
  console.log(bartSay());
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
  function bartSay(){
    return 'eat my shorts'
  };
&lt;/script&gt;
</pre></p>
<p>A pattern for avoiding issues caused by hoisting is described in Stoyan Stefanov excellent book <a href="http://www.amazon.co.uk/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752">JavaScript Patterns</a>. The &#8216;single var&#8217; pattern is the use of a single &#8216;var&#8217; statement at the top of a function, using commas for assigning multiple variables.</p>
<p><pre class="brush: plain;">
function(){
  var bart = 'eat my shorts',
      homer = 'hmm, doughnut',
      say = function(){};
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/404/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=404&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2012/02/13/javascript-scope-hoisting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>SASS: Learning to let go of your CSS sensibilities</title>
		<link>http://jamescryer.wordpress.com/2011/12/04/sass/</link>
		<comments>http://jamescryer.wordpress.com/2011/12/04/sass/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 05:38:16 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[SASS]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[antipattern]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=439</guid>
		<description><![CDATA[SASS to my surprise is really nice way to build large style sheets. It&#8217;s programming-centric approach (as opposed to markup-centric) allows more explicit inheritance patterns making module styles easier to read and easier to maintain. But there is a catch.. My main problem with CSS is that it&#8217;s a markup language with added inheritance. &#8220;DAMN [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=439&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://sass-lang.com/" target="_blank">SASS</a> to my surprise is really nice way to build large style sheets. It&#8217;s programming-centric approach (as opposed to markup-centric) allows more explicit inheritance patterns making module styles easier to read and easier to maintain. But there is a catch..</p>
<p><span id="more-439"></span></p>
<p>My main problem with CSS is that it&#8217;s a markup language with added inheritance. &#8220;DAMN YOU CASCADE!!&#8221;, I&#8217;ve screamed this so many times at work. I spend far too much time fighting legacy code, the cost of re-factoring would be significant so I add another !important or add an ID to the selector. Constantly overriding is a <a title="Call Super anti-pattern" href="http://en.wikipedia.org/wiki/Call_super" target="_blank">bad smell</a>. CSS doesn&#8217;t scale without being explicit.</p>
<p>There is a big mental shift when moving from CSS to SASS. Thinking of things as modules helps.</p>
<p><pre class="brush: plain;">
.clickable {
  color: blue;
  &amp;:hover {
    color: red;
    }
  .ie &amp; {
    padding-left: 1px; //some pretend box-model hack for IE
    }
  }
</pre></p>
<p>The above example uses the SCSS style. Note the use of the ampersand character to denote &#8216;this&#8217;.  SASS allows us to encapsulate state and behaviours within a module in pretty much the same logical way that I would approach a JavaScript module.</p>
<p>Reuse can be achieved with variables, and inheritance patterns using @extend or @include.</p>
<p><pre class="brush: plain;">
$column-width: 256px;

@mixin astract {
  white-space: nowrap;
  overflow: hidden;
  width: $column-width;
  }

.clickable {
  @include abstract;
  color: blue;
  &amp;:hover {
    color: red;
    }
  .ie &amp; {
    padding-left: 1px; //some pretend box-model hack for IE
    }
  }

.special-clickable {
  @extend .clickable;
  border: 1px solid: blue;
  }
</pre></p>
<p>I love that we can achieve <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a> code in this way.  Sure you can extend style modules in CSS, but the relationships are defined explicitly in the HTML not in the stylesheet.  This is a massive discoverability problem. SASS allows us to define these relationships in one place.</p>
<p>This is one way of achieving the same thing in CSS. Note that I&#8217;m also providing a HTML example.</p>
<p><pre class="brush: plain;">
.clickable {
  white-space: nowrap;
  overflow: hidden;
  width: 256px;
  color: blue;
  }
  .clickable:hover {
    color: red;
    }
  .ie .clickable {
    padding-left: 1px; //some pretend box-model hack for IE
    }

.special-clickable {
  border: 1px solid: blue;
  }
</pre><pre class="brush: plain;">
&lt;button class=&quot;clickable special-clickable&quot;&gt;click me&lt;/button&gt;
</pre></p>
<p>As I mentioned earlier, there is a downside to using SASS, <strong>it compiles to CSS</strong>. It produces CSS without the same level of code re-use that you might strive to achieve yourself.  SASS is lazy, especially where mixins are used. Generated CSS becomes bloated quickly, load time of the web page increases.</p>
<p>As with so much in our Industry, it&#8217;s a trade off.  SASS is genuinely a more managable and enjoyable way to style the UI.  But as a tool for building CSS? It&#8217;s as good as it can be, which isn&#8217;t quite good enough.  The CSS bloat issue only begins to appear on larger UI&#8217;s, is that ok? I guess that depends on how bandwidth sensitive your users are.  And selector performance?  I haven&#8217;t noticed any problems though stylistically I would say that SASS encourages deep nesting of selectors which <a href="http://blog.archive.jpsykes.com/152/testing-css-performance-pt-2/" target="_blank">may be an issue</a>. </p>
<p>In this day and age modern web browsers and network infrastructures can easily absorb the extra weight. CSS file sizes generated from SASS are in no way comparable to the files sizes you might expect from many JS libraries.  And CSS selector performance is barely worth monitoring to be honest (Unless your users use IE6).</p>
<p>I&#8217;d recommend you have a play with SASS if you haven&#8217;t already, but you&#8217;re going to have to let go of some of your CSS sensibilities before you begin to enjoy it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/439/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=439&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2011/12/04/sass/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript Scope: Closure</title>
		<link>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-closure/</link>
		<comments>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-closure/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 13:29:08 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[javascript scope]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=402</guid>
		<description><![CDATA[Closures provide a way to hold or trap an activation object in memory. They can be used for defining private variables, and storing variables outside of a functions life-cycle. JavaScript has function-level scope only, so unlike other C languages a &#8216;for&#8217; or &#8216;if&#8217; block won&#8217;t hold scope. To create scope within the if block we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=402&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Closures provide a way to hold or trap an <a href="http://jamescryer.wordpress.com/2011/07/10/javascript-scope-execution-context/" title="JavaScript Scope: Execution Context">activation object</a> in memory.</p>
<p>They can be used for defining private variables, and storing variables outside of a functions life-cycle.</p>
<p><span id="more-402"></span></p>
<p>JavaScript has function-level scope only, so unlike other C languages a &#8216;for&#8217; or &#8216;if&#8217; block won&#8217;t hold scope.</p>
<p><pre class="brush: plain;">
for (var homer in ['doh!','ray','mee']) {
// do something with homer
}

console.log(homer); // logs 'mee'
</pre><br />
<pre class="brush: plain;">
var homer = 'hmmmm donuts';
if(true){
var homer = 'doh!';
// other code
}
console.log(homer); // logs 'doh!'
</pre></p>
<p>To create scope within the if block we could do the following:</p>
<p><pre class="brush: plain;">
var homer = 'hmmmm donuts';
if(true){
(function(){
var homer = 'doh!';
// other code
}());
}
console.log(homer); // 'hmmmm donuts'
</pre></p>
<p>But generally it&#8217;s not good practice to reuse variable names for different purposes.</p>
<p>In the global scope, variables resolve to the global object &#8216;window&#8217;. &#8216;this&#8217; in a global function will be the global object, but variables defined within that function wont resolve to the global object. Functions have there own activation object.</p>
<p><pre class="brush: plain;">
function(){
var homer = 'woo hoo';

var myobject = {
homer: 'spiderpig, spiderpig...',
say: function(){
console.log(homer);
console.log(this.homer);
}
}
myobject.say();
}
</pre></p>
<p>From this example we first log &#8216;woo hoo&#8217; from current function scope then &#8216;spiderpig, spiderpig&#8230;&#8217; as defined on the object.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/402/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=402&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-closure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript Scope: What is this?</title>
		<link>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-what-is-this/</link>
		<comments>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-what-is-this/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 13:15:21 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript scope]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=400</guid>
		<description><![CDATA[&#8216;this&#8217; will always resolve to the global object unless: The containing function is instantiated with the ‘new’ keyword Or.. The function is called on an owner object. Take this example, what do you think will be logged? Answer: &#8216;foobar&#8217; gets logged. &#8216;this&#8217; gets defined when the activation object is created for the current execution. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=400&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>&#8216;this&#8217; will always resolve to the global object unless:</p>
<p><strong>The containing function is instantiated with the ‘new’ keyword</strong></p>
<p><pre class="brush: plain;">
var me = this;
function Test(){
	console.log(this === me);
}
var myTest = new Test();  // false
</pre></p>
<p>Or.. <strong>The function is called on an owner object.</strong></p>
<p><pre class="brush: plain;">
var obj = {
myFunc: function(){
return this;
}
};
console.log( this === obj.myFunc() ); // false
</pre></p>
<p><span id="more-400"></span></p>
<p>Take this example, what do you think will be logged?</p>
<p><pre class="brush: plain;">
var i = {
myvar: 'something',
invoke: function(callback){
callback();
}
};

var obj1 = {
myvar: 'default',
counter: function(){
if(!this.myvar){
this.myvar = 'foobar';
}
console.log(this.myvar);
}
}

i.invoke(obj1.counter);
</pre></p>
<p>Answer: &#8216;foobar&#8217; gets logged.</p>
<p>&#8216;this&#8217; gets defined when the <a href="http://jamescryer.wordpress.com/2011/07/10/javascript-scope-execution-context/" title="JavaScript Scope: Execution Context" target="_blank">activation object</a> is created for the current execution. The function reference is executed without an owner object so this will resolve to the global object;</p>
<p>What&#8217;s worse about this is that window now has a new property called myvar.</p>
<p>To complicate matters further you can explicitly define &#8216;this&#8217;. Either by (re)assigning the method to the desired owner method or by using the call and apply methods.</p>
<p><pre class="brush: plain;">
i.counter = obj.counter;
i.counter() // 'something'

obj1.counter.call(i); // 'something'
obj1.counter.apply(i, []); // 'something'
</pre></p>
<p>They are basically the same method except that apply() will let you pass in the arguments object (or an array of arguments).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/400/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=400&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-what-is-this/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript Scope: Execution Context</title>
		<link>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-execution-context/</link>
		<comments>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-execution-context/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 13:11:23 +0000</pubDate>
		<dc:creator>jamescryer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript scope]]></category>
		<category><![CDATA[window object]]></category>

		<guid isPermaLink="false">http://jamescryer.wordpress.com/?p=398</guid>
		<description><![CDATA[The Execution Context is an abstract representation of the current runtime environment in JavaScript. To explain a little better, the Execution Context is formed of three components. Lexical Environment Variable Environment This binding Perhaps a better way to think of this is: Scope chain Local variables &#8216;this&#8217; keyword The scope chain represents an array of parent [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=398&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" target="_blank">The Execution Context</a> is an abstract representation of the current runtime environment in JavaScript. To explain a little better, the Execution Context is formed of three components.</p>
<ul>
<li>Lexical Environment</li>
<li>Variable Environment</li>
<li>This binding</li>
</ul>
<p><span id="more-398"></span></p>
<div>Perhaps a better way to think of this is:</div>
<div>
<ul>
<li>Scope chain</li>
<li>Local variables</li>
<li>&#8216;this&#8217; keyword</li>
</ul>
</div>
<p>The scope chain represents an array of parent &#8216;Variable environments&#8217; up to and including the global variable environment.</p>
<p>Each Variable environment is in fact an object called an activation (or variable) object. Though not a real object in the normal sense, it does have properties which can be accessed.</p>
<p><pre class="brush: plain;">
{
__parent__ :[Object reference],
arguments: [Lots of fun that I wont go into now]
this: [Object reference]
mylocalVar: [undefined]
}
</pre></p>
<p>The above example represents the activation object after the initialisation of the current context, the myLocalVar property is a local variable.  At this point the activation object is sometimes referred to as the Variable object, as far as I understand they are essentially the same thing (if for the moment we pretend the with() method doesn&#8217;t exist).</p>
<p>The &#8216;this&#8217; property  refers to the owner object (by default this is the global object .i.e. window).</p>
<p>The __parent__ property is a reference to the parent activation object. This object is not explicitly accessible, though access to properties on this object are implied from the scope chain.</p>
<p>The global context is some what confused, &#8216;this&#8217; resolves to the activation object itself thus&#8230;</p>
<p><pre class="brush: plain;">

var foo = 'bar'
console.log(window.foo); // bar
console.log(this.foo); // bar

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamescryer.wordpress.com/398/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamescryer.wordpress.com/398/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamescryer.wordpress.com&#038;blog=6821554&#038;post=398&#038;subd=jamescryer&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamescryer.wordpress.com/2011/07/10/javascript-scope-execution-context/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/713f28722c6c5988c5bae2e44eda20b2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jamescryer</media:title>
		</media:content>
	</item>
	</channel>
</rss>
