<?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>Diego M. Quinteiro - Webmaster</title>
	<atom:link href="http://webmaster.diegoquinteiro.com/lang/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://webmaster.diegoquinteiro.com</link>
	<description>just another boring blog for webmasters</description>
	<lastBuildDate>Wed, 10 Feb 2010 04:54:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Private members in JavaScript</title>
		<link>http://webmaster.diegoquinteiro.com/lang/en/2010/membros-privados-no-javascript</link>
		<comments>http://webmaster.diegoquinteiro.com/lang/en/2010/membros-privados-no-javascript#comments</comments>
		<pubDate>Wed, 10 Feb 2010 03:30:38 +0000</pubDate>
		<dc:creator>diegoquinteiro</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://webmaster.diegoquinteiro.com/?p=85</guid>
		<description><![CDATA[

Encapsulation is the most important achievement of object oriented programming. Using a piece of software without worrying about its implementation makes possible for our limited brain to understand and code complex applications.
A simple example in Java:

public class Point {
    private double x;
    private double y;
    public [...]]]></description>
			<content:encoded><![CDATA[<p><br />
<br />
Encapsulation is the most important achievement of object oriented programming. Using a piece of software without worrying about its implementation makes possible for our limited brain to understand and code complex applications.</p>
<p>A simple example in Java:</p>
<pre class="brush: java;">
public class Point {
    private double x;
    private double y;
    public Ponto (double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double distanceFromOrigin () {
        return Math.sqrt(x * x + y * y);
    }
}
</pre>
<p>Anyone who wants to use this class doesn&#8217;t need to know how the point coordinates is stored or how the distance from the origin is calculated. The information is encapsulated. Encapsulation improves abstraction and simplifies our code.</p>
<p>In order to encapsulate properly, we need private members, i.e. variables that can&#8217;t be read or changed from outside our software piece.</p>
<p>In more traditional languages such as Java, it is done with explicit access control. By using the <em>private</em> keyword, we prevent the variable from being accessed outside its own class. But how can we do the same with JavaScript, since it doesn&#8217;t even have classes?</p>
<p>In fact, it&#8217;s very simple. Variables defined within a function are confined to the function scope. As discussed in my <a href="http://webmaster.diegoquinteiro.com/2010/escopo-e-closures-no-javascript">last post about closures</a>, inner functions have access to their outer function&#8217;s scope. Putting it all together, we have what we want:</p>
<pre class="brush: jscript;">
var Point = function (x, y) {
    this.distanceFromOrigin = function () {
        return Math.sqrt(x * x + y * y);
    }
}

var point = new Point(3, 4);
alert(point.distanceFromOrigin()); // alerts &quot;5&quot;

alert(point.x); // alerts &quot;undefined&quot;
alert(point.y); // alerts &quot;undefined&quot;
</pre>
<p>That way, <em>x</em> and <em>y</em> are private members. They are local variables of the constructor and would not exist outside it. When <em>distanceFromOrigin</em> is assigned to the <em>this</em> object, which is returned, a closure is created so that function will keep permanent access to that local variables.</p>
<p>The cost of using this technique is that with every execution of the constructor the inner functions are recreated. One instance of each method for each object instance. Usually this is not a problem at all, but watch out for objects that have hundreads or thousands of instances.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://webmaster.diegoquinteiro.com/lang/en/2010/membros-privados-no-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript closures and scope</title>
		<link>http://webmaster.diegoquinteiro.com/lang/en/2010/escopo-e-closures-no-javascript</link>
		<comments>http://webmaster.diegoquinteiro.com/lang/en/2010/escopo-e-closures-no-javascript#comments</comments>
		<pubDate>Sat, 30 Jan 2010 18:55:31 +0000</pubDate>
		<dc:creator>diegoquinteiro</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[object oriented programming]]></category>

		<guid isPermaLink="false">http://webmaster.diegoquinteiro.com/?p=83</guid>
		<description><![CDATA[

Get to know the scope of your variables. In JavaScript only functions can define scopes and only variables explicit declared by using the var keyword are restricted to local scopes, otherwise they&#8217;re global. Furthermore, function parameters are also local variables. Pay attention to this example:

function example (arg) {
	var localVariable = 0;
	if (arg &#62; 0) {
		var [...]]]></description>
			<content:encoded><![CDATA[
<p><br />
Get to know the scope of your variables. In JavaScript only functions can define scopes and only variables explicit declared by using the <em>var</em> keyword are restricted to local scopes, otherwise they&#8217;re global. Furthermore, function parameters are also local variables. Pay attention to this example:</p>
<pre class="brush: jscript;">
function example (arg) {
	var localVariable = 0;
	if (arg &gt; 0) {
		var sameScope = arg + 1;
	}
	localVariable = sameScope;
	alert(localVariable);
	globalVariable = arg;
}

example(1); // Alerts &quot;2&quot;

alert(localVariable); // Alerts &quot;undefined&quot;

alert(sameScopo); // Alerts &quot;undefined&quot;

alert(globalVariable); // Alerta &quot;1&quot;
</pre>
<p>Notice that the <em>sameScope</em> variable could be assinged to <em>localVariable</em> althought it was declared inside an <em>if</em> block. Blocks doesn&#8217;t define scopes, only functions do.</p>
<p>JavaScript always defines a global object, which in the browsers is the <em>window</em> object. All global variables are in fact properties of this object, so:</p>
<pre class="brush: jscript;">
example(0);

window.example(1); // functions are also variables

alert(globalVariable); // Alerts &quot;1&quot;

alert(window.globalVariable); // Alerts &quot;1&quot; too

alert(globalVariable === window.globalVariable); // Alerts &quot;true&quot;
</pre>
<h3>Closures</h3>
<p>Functions in JavaScript are first-class objects. That means you can pass a function to other functions, assign functions to variables or use them as return value. A declaration <em>function functionName (arg1, arg2, &#8230;) { &#8230; body &#8230; }</em> have the same effect as <em>var functionName = function (arg1, arg2, &#8230;) { &#8230; body &#8230; };</em>.</p>
<p>The inner functions <strong>always</strong> have access to the outer function&#8217;s variables. For example:</p>
<pre class="brush: jscript;">
var outerFunction = function (value) {
    var outerFunctionVariable = value;
    var innerFunction = function () {
        alert(outerFunctionVariable);
    }
    innerFunction();
}

outerFunction(1); // Alerts &quot;1&quot;;
</pre>
<p>Something curious happens when we keep a reference to the inner function after the outer function execution:</p>
<pre class="brush: jscript;">
var outerFunction = function (value) {
    var outerFunctionVariable = value;
    var innerFunction = function () {
        alert(outerFunctionVariable);
    }
    return innerFunction;
}

var inner1 = outerFunction(1);
var inner2 = outerFunction(2);

inner1(); // Alerts &quot;1&quot;!
inner2(); // Alerts &quot;2&quot;!
</pre>
<p>That way we created a <em>closure</em>. </p>
<p>Local variables created by the outer function weren&#8217;t destroyed even after its execution. By keeping a reference to <em>innerFunction</em>, we also prevented the <em>outerFunction</em> variables to die.</p>
<p>This is a powerful feature of the language, as we will see in next posts.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://webmaster.diegoquinteiro.com/lang/en/2010/escopo-e-closures-no-javascript/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript constructors</title>
		<link>http://webmaster.diegoquinteiro.com/lang/en/2010/construtores-em-javascript</link>
		<comments>http://webmaster.diegoquinteiro.com/lang/en/2010/construtores-em-javascript#comments</comments>
		<pubDate>Sat, 30 Jan 2010 07:30:19 +0000</pubDate>
		<dc:creator>diegoquinteiro</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[object oriented programming]]></category>

		<guid isPermaLink="false">http://webmaster.diegoquinteiro.com/?p=81</guid>
		<description><![CDATA[

Fact: JavaScript is an object-oriented language.
So you can ask: where are the damn classes?
The truth is that there are no classes. JavaScript objects are soft: one can freely add, modify or remove properties from each of them individually in runtime. It&#8217;s a diferent way of programming object oriented, and a very powerful way.
The new operator
To [...]]]></description>
			<content:encoded><![CDATA[
<p><br />
Fact: JavaScript <strong>is</strong> an object-oriented language.</p>
<p>So you can ask: where are the damn classes?</p>
<p>The truth is that there are no classes. JavaScript objects are soft: one can freely add, modify or remove properties from each of them individually in runtime. It&#8217;s a diferent way of programming object oriented, and a very powerful way.</p>
<h3>The <em>new</em> operator</h3>
<p>To master object orientation in JavaScript you need to clearly understand the <em>new</em> operator behaviour. This operators is used to create and initialize a new object given a function, called &#8220;constructor function&#8221;. For example:</p>
<pre class="brush: jscript;">
function Person(name) {
	this.name = name;
}

var diego = new Person(&quot;Diego&quot;);
alert(diego.name); // Alerts &quot;Diego&quot;

var gabriel = new Person(&quot;Gabriel&quot;);
alert(gabriel.name); // Alerts &quot;Gabriel&quot;
</pre>
<p>Notice that <em>diego</em> and <em>gabriel</em> are <strong>not</strong> instances of Person class. Indeed, there&#8217;s no Person class, just a function named Person. Remember: <em>there are no classes in JavaScript</em></p>
<p>When the <em>new</em> operator is used, an empty object is created and passed to the constructor function as the <em>this</em> reference. The function then can add members to that object by simple assignment, as in <em>this.name = name</em>.</p>
<p>Unless the function returns a not null object, the object <em>this</em> is returned and a reference for the constructor function is stored in the <em>constructor</em> property.</p>
<p>The result is very similar to class instantiation and we are tempted to call <em>Person</em> a class. It would be a real class if the object couldn&#8217;t be modified after its creation. JavaScript doesn&#8217;t works that way: objects can have properties and methods removed or modified anytime after their construction.</p>
<p>For didactic purpose, we&#8217;ll now implement the JavaScript <em>new</em> operator as a function:</p>
<pre class="brush: jscript;">
var operatorNew = function (constructorFunction, args) {

	var object = {}, // creates an empty object
	    returnValue;

	// calls the function passing the object as 'this'.
	returnValue = constructorFunction.apply(object, args);

	if ((typeof(returnValue) !== &quot;object&quot; &amp;&amp;
	    typeof(returnValue) !== &quot;function&quot;) ||
	    returnValue === null) {

		// if returnValue is not a not-null object returns 'this'
		returnValue = object;
		returnValue.constructor = constructorFunction;

	}

	return returnValue;
}
</pre>
<p>So the call&#8230;</p>
<pre class="brush: jscript;">
var object = new ConstructorFunction(arg1, arg2, ...);
</pre>
<p>&#8230; will be the same as</p>
<pre class="brush: jscript;">
var object = operatorNew(ConstructorFunction, [arg1, arg2, ..]);
</pre>
<p>&#8230; for any ConstructorFunction implemented in JavaScript.</p>
<p>PS: It won&#8217;t work with primitive types (String, Array etc). These are implemented in the browser, not in JavaScript code.</p>
<p>Analysing the <em>operatorNew</em> function we created, we realize that the <em>new</em> operator is just a shortcut. We can always create objects from literal notation then add members freely using no constructor at all.</p>
<p>Look at the following code:</p>
<pre class="brush: jscript;">
var diego1 = new Person(&quot;Diego&quot;);

var diego2 = {}
diego2.name = &quot;Diego&quot;;
diego2.constructor = Person;

var diego3 = {constructor: Person, name: &quot;Diego&quot;};

var diego4 = operatorNew(Person, [&quot;Diego&quot;]);
</pre>
<p>The objects <em>diego1</em>, <em>diego2</em>, <em>diego3</em> and <em>diego4</em> are equal!</p>
<p>Make no mistake: objects and construtors are in, classes are out.</p>
<p>That&#8217;s the JavaScript way.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://webmaster.diegoquinteiro.com/lang/en/2010/construtores-em-javascript/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting Real: cure rather then prevent!</title>
		<link>http://webmaster.diegoquinteiro.com/lang/en/2008/getting-real-caindo-na-real-e-melhor-remediar-que-prevenir</link>
		<comments>http://webmaster.diegoquinteiro.com/lang/en/2008/getting-real-caindo-na-real-e-melhor-remediar-que-prevenir#comments</comments>
		<pubDate>Wed, 02 Jul 2008 21:40:30 +0000</pubDate>
		<dc:creator>diegoquinteiro</dc:creator>
				<category><![CDATA[Sem Categoria]]></category>
		<category><![CDATA[methodology]]></category>
		<category><![CDATA[getting real]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://owebmaster.wordpress.com/?p=66</guid>
		<description><![CDATA[




The messages from Getting Real book are quite unusual. Some of the concepts defended by the 37signals gurus, the creators of Ruby on Rails, seems to go against everything we learned:

Get real!
Toss your crystal ball: don&#8217;t wast your today solving tomorrow&#8217;s issues &#8211; you have enough problems to worry about today.
Your working project must be [...]]]></description>
			<content:encoded><![CDATA[




<p><span lang="en">The messages from Getting Real book are quite unusual. Some of the concepts defended by the <a title="37signals is the company behind Ruby on Rails framework">37signals</a> gurus, the creators of Ruby on Rails, seems to go against everything we learned:</span></p>
<ul>
<li><span lang="en">Get real!</span></li>
<li><span lang="en">Toss your crystal ball: don&#8217;t wast your today solving tomorrow&#8217;s issues &#8211; you have enough problems to worry about today.</span></li>
<li><span lang="en">Your working project must be your best if not only documentation. Don&#8217;t create documents that never come true.</span></li>
<li><span lang="en">Avoid meetings. When they are absolutely necessary, set your mobile alarm to 30 minutes: when it rings, meeting is over.</span></li>
<li><span lang="en">Keep in mind that errors will occur. Don&#8217;t worry: it&#8217;s a web system, not a cerebral surgery.</span></li>
</ul>
<p><span lang="en">It&#8217;s a very shocking approach that some may disagree, but it&#8217;s surely valuable to all.</span></p>
<p><span lang="en"><a href="http://gettingreal.37signals.com/toc.php">The full text is available for free, take a look.</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://webmaster.diegoquinteiro.com/lang/en/2008/getting-real-caindo-na-real-e-melhor-remediar-que-prevenir/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

