<?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>neilgoodman.net &#187; Web</title>
	<atom:link href="http://neilgoodman.net/category/web/feed/" rel="self" type="application/rss+xml" />
	<link>http://neilgoodman.net</link>
	<description>I am currently a Software Design Engineer at Sonic Foundry. I primarily develop front-end web applications and Android apps in my spare time. If you would like to contact me, please use one of the social networks listed below.</description>
	<lastBuildDate>Sun, 15 Jul 2012 18:04:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Five Important Things to Know in Web Development</title>
		<link>http://neilgoodman.net/2012/05/05/five-important-things-to-know-in-web-development/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=five-important-things-to-know-in-web-development</link>
		<comments>http://neilgoodman.net/2012/05/05/five-important-things-to-know-in-web-development/#comments</comments>
		<pubDate>Sat, 05 May 2012 23:07:45 +0000</pubDate>
		<dc:creator>Neil Goodman</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[positioning]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://neilgoodman.net/?p=254</guid>
		<description><![CDATA[Getting back into web development a few months ago inspired me to write a list of topics on the subject. I came up with five things that I think are important for any web developer to at least be aware of. The List There are certainly more than five things to know when studying web [...]]]></description>
				<content:encoded><![CDATA[<p>Getting back into web development a few months ago inspired me to write a list of topics on the subject. I came up with five things that I think are important for any web developer to at least be aware of.</p>


<span id="more-254"></span>





<h3>The List</h3>

<p>There are certainly more than five things to know when studying web development, but I came up with this list by choosing the skills that changed my development pattern from wild random hacking, to actual engineering.</p>

<p>I decided to focus on topics that would be useful to people just starting out, but at the same time were not completely trivial.</p>

<h3>1. Clearing containers with <code>overflow: hidden;</code></h3>

<p>I used to insert <code>&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;</code> tags all over my HTML when dealing with floated elements. I stopped doing this after I learned a pretty useful trick: if a layout container in an HTML document is given the <code>overflow: hidden;</code> property, it will wrap around its floated children, producing the same effect as the <code>&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;</code> solution. See my example <a href="http://jsfiddle.net/hr8GT/">here</a> on jsFiddle.</p>

<p><strong>Note:</strong> This is a useful trick, but I currently use LESS and Twitter Bootstrap in a lot of my projects. In that environment I just rely on the <code>.clearfix()</code> mixin. Check out LESS here: <a href="http://lesscss.org/">http://lesscss.org/</a> and Twitter Bootstrap here: <a href="http://twitter.github.com/bootstrap/">http://twitter.github.com/bootstrap/</a>.

<h3>2. CSS <code>position</code> and <code>z-index</code></h3>

<p>CSS positioning allows you to move elements around the page using offsets. For example:</p>

<pre><code>#container {
    position: relative;
    top: 10px;
    left: 10px;
}
</code></pre>

<p>The above CSS offsets the #container element 10 pixels from its top and left edge in normal flow. Normal flow is the default rendering scheme for elements. There are CSS properties that can remove an element from normal flow, which allows for manual placement. This happens most often with elements that are floated or elements that have a position value set to absolute.</p>

<p>Absolute positioning is probably one of the most misunderstood concepts in CSS. I struggled with it for a while, but once you understand all the nuances, then it really isn't that difficult of a concept. Here are the basics:</p>

<ul>
	<li>All elements have a default position of static.</li>
	<li>Any element with the position of relative will be offset from its normal position. You specify offsets using the top, left, right, and bottom properties.</li>
	<li>Any element with the position of absolute will be offset from its first ancestor that has a position that is not static. An ancestor is any container element. For example, any <code>&lt;p&gt;</code> tag on this blog post has the <code>&lt;body&gt;</code> tag as an ancestor, because all <code>&lt;p&gt;</code> tags are inside of the <code>&lt;body&gt;</code> tag. All <code>&lt;p&gt;</code> tags are also inside of the <code>&lt;html&gt;</code> tag, so <code>&lt;html&gt;</code> is also an ancestor of <code>&lt;p&gt;</code>.</li>
	<li>Any element with the position of absolute is taken out of the normal flow of the document, which means that the margin and padding of any neighboring element do not impact the absolutely positioned element.</li>
</ul>

<p>There is another concept that is tied to using the position property in CSS, and that is the z-index property. This property allows you to lay elements on top of others. If you come from a math or engineering background, then you can think of the z-index as being an axis that is orthogonal to the screen. If you come from a design background, you can think of the z-index as being a layer number. In either case, elements with a higher z-index number will appear on top of elements with a lower z-index number. The reason this property is related to the position property is because you can only give z-index values to elements that have a position value other than static.</p>

<p>There is one last gotcha with position absolute. If you absolutely position an element, but you do not give it a top, left, right, or bottom value, then it will remain in the position that the browser would normally place it in. However, the element will still be taken out of the normal flow of the document, which means that its containers will not wrap around it, neighboring elements will not impact it with margin or padding values, and elements can appear above or beneath it. This style of absolute positioning is often used with drop-down menus, so that the drop-downs appear above all other content on the page, but otherwise appear where you would expect them to in the document.</p>

<p><a href="http://jsfiddle.net/qRDBV/">Here</a> is a jsFiddle with a position example.</p>

<p>If everything I just said made no sense to you, then read over some of these resources. One of them is bound to resonate with you. This is a really important concept to get down:</p>

<ul>
	<li><a href="http://css-tricks.com/absolute-positioning-inside-relative-positioning">http://css-tricks.com/absolute-positioning-inside-relative-positioning</a></li>
	<li><a href="http://coding.smashingmagazine.com/2009/10/05/mastering-css-coding-getting-started/#CSS-Basics7">http://coding.smashingmagazine.com/2009/10/05/mastering-css-coding-getting-started/#CSS-Basics7</a></li>
	<li>For the brave (or engineer): <a href="http://www.w3.org/TR/CSS2/visuren.html">http://www.w3.org/TR/CSS2/visuren.html</a></li>
</ul>

<h3>3. CSS Specificity</h3>

<p>I went a long time without knowing about CSS specificity. Those days were a nightmare.</p>

<p>The bottom line: not all CSS selectors are equal. Each selector is given a weight and selectors with higher weight will override the styles contained in selectors with lower weight. Example:</p>

<p><strong>CSS:</strong></p>

<pre><code>#too-bad-youre-blue {
    background-color: blue;
}

.i-want-you-red {
    background-color: red;
}
</code></pre>

<p><strong>HTML:</strong></p>

<pre><code>&lt;div id=&quot;too-bad-youre-blue&quot; class=&quot;i-want-you-red&quot;&gt;
    &lt;p&gt;I am blue!&lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>So, what is happening here? Well, we are trying to make the above <code>&lt;div&gt;</code> tag have a background-color of red and even though we wrote a valid selector that points to our <code>&lt;div&gt;</code> tag, it will still have a background of blue. Why? Because the <code>#too-bad-youre-blue</code> selector is selecting the same element using an ID selector, and ID selectors have a greater weight (or in CSS terms, a greater specificity). So, at the end of the day the ID selector is going to win.</p>

<p>What can you do to override the blue background? Each component of a selector impacts its specificity. The rule I learned was to give each ID in the selector a value of 100, each class a value of 10, and each tag a value of 1. For example:</p>

<pre><code>/* This selector has a specificity of 111: (body = 1) + (#container = 100) + (.items = 10) = 111 */
body #container .items {
    font-weight: bold;
}

/* This selector has a specificity of 10: (.items = 10) = 10 */
.items {
    font-weight: normal;
}
</code></pre>

<p>In the above example, the first selector will make all text inside of the .items class bold, because the second selector has a smaller specificity. So, to make the <code>&lt;div&gt;</code> blue in the first example, we could use the following selector:</p>

<pre><code>/* Notice that there is no space between the ID selector and class selector. This is how you select an
   element that has both an ID of too-bad-youre-blue and a class of i-want-you-red. */
#too-bad-youre-blue.i-want-you-red {
    background-color: red;
}
</code></pre>

<p>Here is a great article to learn more about specificity: <a href="http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know">http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know</a></p> 

<h3>4. JavaScript Prototypes</h3>

<p>JavaScript is a fully object-oriented language, but many developers coming from other object-oriented languages (such as Java) can have trouble adapting to JavaScript because it doesn't use classes. What JavaScript offers instead is prototype inheritance.</p>

<p>In JavaScript every object has a property known as a prototype. There is no cross-browser way to access this prototype, but you can access it in WebKit based browsers and Firefox by examining the __proto__ property. __proto__ will either be another object or null. Objects in __proto__ also have a __proto__ attribute. This defines a list of objects which is know as the prototype chain.</p>

<p>So what does the prototype actually do? If a method is called on an object that is undefined, then its prototype will be checked. If the prototype has the method, then it is called, otherwise the next prototype is checked and so on. This is also true for properties. This is easier to visualize in an example:</p>

<pre><code>var baseObject  = {},
    superObject = {
        aMethod: function () {
            alert(&#039;Hello!&#039;);
        }
    };

baseObject.__proto__ = superObject; // Only use this in WebKit or Firefox.

baseObject.aMethod(); // This will popup an alert that says hello.
</code></pre>

<p>The only cross-browser way to set the prototype of an object is to use JavaScript's "constructor" mechanism:</p>

<pre><code>// Define a simple function.
function MyFunction () {
}

// All functions are objects in JavaScript. Each function has a special property called prototype. When
// a function is used with the new operator, the object in its prototype property is placed into
// the __proto__ property of the new object. __proto__ will contain a reference to this object,
// so a change to the MyFunction.prototype property will effect all instances of MyFunction.
MyFunction.prototype = {
    aValue: &#039;Hello!&#039;
};

// You will see this line in a lot of JavaScript code. This doesn&#039;t really do anything
// useful other than support some JavaScript programmers who like to test object types by using
// the constructor property: (obj.constructor === MyFunction). This is considered bad practice
// by many JavaScript programmers because the instanceof operator is a better way make this
// type of test. Still, why not just support everyone.
MyFunction.prototype.constructor = MyFunction;

// Use the new operator to create a new object using MyFunction as a constructor.
var instance = new MyFunction();

alert(instance.aValue); // This will output &#039;Hello!&#039;
</code></pre>

<p>JavaScript's implementation of prototype based inheritance is often regarded as a major failure of the language's design, because it mixes in class inheritance syntax. Many JavaScript programmers use inheritance frameworks. Here is one from Google's Closure Library: <a href="http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html#goog.inherits">http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html#goog.inherits</a></p>

<h3>5. JavaScript Closures</h3>

<p>In JavaScript you can define a function inside of another function, which causes some interesting scope issues:</p>

<pre><code>// A basic example of a function that uses a variable defined in global scope.
var iAmAVariable = &#039;Storing a string&#039;;
function basicFunction() {
    console.log(iAmAVariable);
}

// A nested function example.
function nestedFunction() {
    var aLocalVariable = &#039;So local&#039;;

    var iAmTheNest = function () {
        console.log(aLocalVariable);
    }

    return iAmTheNest;
}

var testTheNest = nestedFunction();
testTheNest(); // This will output &#039;So local&#039; on the log.
</code></pre>

<p>Notice how the <code>iAmTheNest()</code> function uses a local variable from the <code>nestedFunction()</code>. When <code>iAmTheNest()</code> was returned and stored in <code>testTheNest</code> and then executed, it was still able to access the <code>aLocalVariable</code> value even though <code>nestedFunction()</code> had already returned. This is because when a function is defined inside of another function in JavaScript, it is given a reference to all the local variables that existed in the outer function it was defined in. This reference is what is called a closure. So even though those local variables no longer live on the call stack, they do live inside of the closure that is being referenced by the nested function.</p>

<p>How is this useful? One use is with nested event handlers. Here is a jQuery example:</p>

<pre><code>// This is common jQuery boilerplate, but it is an event that is fired once the DOM
// has finished loading. So, this fires a callback function.
$(document).ready(function () {
    var iLiveInThisFunction = &#039;Value&#039;;

    $(&#039;body&#039;).on(&#039;click&#039;, function (event) {
        // iLiveInThisFunction also lives in this handler&#039;s closure, so
        // it can be safely referenced when this handler is executed at a later
        // time.
        console.log(iLiveInThisFunction);
    });
});
</code></pre>

<p>That's it. Let me know if anything I said was horribly wrong or if you have any questions.</p>]]></content:encoded>
			<wfw:commentRss>http://neilgoodman.net/2012/05/05/five-important-things-to-know-in-web-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
