<?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>JavaScript programming language &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/javascript-programming-language/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>You think you know javascript. Quiz results!</title>
		<link>/2012/03/07/you-think-you-know-javascript-quiz-results/</link>
		<comments>/2012/03/07/you-think-you-know-javascript-quiz-results/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 10:27:37 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[quiz]]></category>
		<category><![CDATA[Client-side JavaScript]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[JavaScript syntax]]></category>
		<category><![CDATA[JScript]]></category>
		<category><![CDATA[Scripting languages]]></category>

		<guid isPermaLink="false">/?p=2854</guid>
		<description><![CDATA[After a week again, the &#8220;You think you know &#8230; JavaScript&#8221; quiz results are on! 1. Which loop is faster in JavaScript? for while correct answer 2. What will be alerted by the code bellow? var str = 'my-'; alert(str.split('-')[1]); Error An empty string correct answer &#8220;-&#8220; &#8220;my&#8221; 3. What is server-side JavaScript? JS scripts &#8230; <a href="/2012/03/07/you-think-you-know-javascript-quiz-results/" class="more-link">Continue reading <span class="screen-reader-text">You think you know javascript. Quiz results!</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/02/29/you-think-you-know-algorithms-quiz-results/" rel="bookmark" title="You think you know algorithms. Quiz results!">You think you know algorithms. Quiz results! </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2012/05/09/you-think-you-know-algorithms-quiz-results-2/" rel="bookmark" title="You think you know algorithms. Quiz results!">You think you know algorithms. Quiz results! </a></li>
<li><a href="/2012/01/24/javascript-performance-for-vs-while/" rel="bookmark" title="JavaScript Performance: for vs. while">JavaScript Performance: for vs. while </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>After a week again, the &#8220;You think you know &#8230; JavaScript&#8221; quiz results are on!</p>
<p><strong>1. Which loop is faster in JavaScript?</strong></p>
<ul>
<li>for</li>
<li>while <span style="color: #339966;">correct answer</span></li>
</ul>
<p><figure id="attachment_2883" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/answer1.png"><img src="/wp-content/uploads/2012/03/answer1.png" alt="Answers of the first question chart" title="Answers of the first question chart" width="600" height="371" class="size-full wp-image-2883" srcset="/wp-content/uploads/2012/03/answer1.png 600w, /wp-content/uploads/2012/03/answer1-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text"> </figcaption></figure><br />
<span id="more-2854"></span><br />
<strong>2. What will be alerted by the code bellow? </strong></p>
<pre lang="javascript">var str = 'my-'; alert(str.split('-')[1]);</pre>
<ul>
<li>Error</li>
<li>An empty string <span style="color: #339966;">correct answer</span></li>
<li>&#8220;-&#8220;</li>
<li>&#8220;my&#8221;</li>
</ul>
<figure id="attachment_2885" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/answer2.png"><img src="/wp-content/uploads/2012/03/answer2.png" alt="Answers of the second question chart" title="Answers of the second question chart" width="600" height="371" class="size-full wp-image-2885" srcset="/wp-content/uploads/2012/03/answer2.png 600w, /wp-content/uploads/2012/03/answer2-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text"> </figcaption></figure>
<p><strong>3. What is server-side JavaScript?</strong></p>
<ul>
<li>JS scripts files placed on the server that can be publicly accessed by the client</li>
<li>JS engine running on the server accessible via http <span style="color: #339966;">correct answer</span></li>
</ul>
<figure id="attachment_2887" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/answer3.png"><img src="/wp-content/uploads/2012/03/answer3.png" alt="Answers of the third question chart" title="Answers of the third question chart" width="600" height="371" class="size-full wp-image-2887" srcset="/wp-content/uploads/2012/03/answer3.png 600w, /wp-content/uploads/2012/03/answer3-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text">90.91% right answers</figcaption></figure>
<p><strong>4. Is it possible to have private and public methods in a JavaScript object</strong></p>
<ul>
<li>Yes <span style="color: #339966;">correct answer</span></li>
<li>No</li>
</ul>
<figure id="attachment_2889" style="width: 540px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/answer4.png"><img src="/wp-content/uploads/2012/03/answer4.png" alt="Answers of the fourth question chart" title="Answers of the fourth question chart" width="540" height="331" class="size-full wp-image-2889" srcset="/wp-content/uploads/2012/03/answer4.png 540w, /wp-content/uploads/2012/03/answer4-300x183.png 300w" sizes="(max-width: 540px) 100vw, 540px" /></a><figcaption class="wp-caption-text">54.55% answered with the right answer!</figcaption></figure>
<p><em>Note that the quiz will remain open, but no results will be published!</em></p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/02/29/you-think-you-know-algorithms-quiz-results/" rel="bookmark" title="You think you know algorithms. Quiz results!">You think you know algorithms. Quiz results! </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2012/05/09/you-think-you-know-algorithms-quiz-results-2/" rel="bookmark" title="You think you know algorithms. Quiz results!">You think you know algorithms. Quiz results! </a></li>
<li><a href="/2012/01/24/javascript-performance-for-vs-while/" rel="bookmark" title="JavaScript Performance: for vs. while">JavaScript Performance: for vs. while </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/03/07/you-think-you-know-javascript-quiz-results/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>JavaScript Performance: for vs. while</title>
		<link>/2012/01/24/javascript-performance-for-vs-while/</link>
		<comments>/2012/01/24/javascript-performance-for-vs-while/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 14:20:05 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Control flow]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[Increment]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[software/hardware]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[While loop]]></category>

		<guid isPermaLink="false">/?p=2635</guid>
		<description><![CDATA[JavaScript Loops If you have read some preformance tests on JavaScript loops, you may have heard that &#8220;while&#8221; is faster than &#8220;for&#8221;. However the question is how faster is &#8220;while&#8221;? Here are some results, but first let&#8217;s take a look on the JavaScript code. The for experiment console.time('for'); for (var i = 0; i < &#8230; <a href="/2012/01/24/javascript-performance-for-vs-while/" class="more-link">Continue reading <span class="screen-reader-text">JavaScript Performance: for vs. while</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/02/02/firebugs-console-time-accuracy/" rel="bookmark" title="Firebug&#8217;s console.time() accuracy">Firebug&#8217;s console.time() accuracy </a></li>
<li><a href="/2010/02/02/profiling-javascript-with-firebug-console-profile-console-time/" rel="bookmark" title="Profiling JavaScript with Firebug. console.profile() &#038; console.time()!">Profiling JavaScript with Firebug. console.profile() &#038; console.time()! </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/06/02/detecting-pressed-key-with-e-which-in-javascript/" rel="bookmark" title="Detecting Pressed Key with e.which in JavaScript">Detecting Pressed Key with e.which in JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>JavaScript Loops</h2>
<p>If you have read some preformance tests on JavaScript loops, you may have heard that &#8220;while&#8221; is faster than &#8220;for&#8221;. However the question is how faster is &#8220;while&#8221;? Here are some results, but first let&#8217;s take a look on the JavaScript code. </p>
<h4>The <strong>for</strong> experiment</h4>
<pre lang="javascript">
console.time('for');
for (var i = 0; i < 10000000; i++) {
	i / 2;
}
console.timeEnd('for');
</pre>
<h4>The <strong>while</strong> experiment</h4>
<pre lang="javascript">
console.time('while');
var i = 0;
while (i++ < 10000000) {
	i / 2;
}
console.timeEnd('while');
</pre>
<p>Note - these tests are performed and measured with Firebug on Firefox.</p>
<h2>Results</h2>
<p>It's a fact, that you'll get different results as many times as you run this snippet. It depends also on the enviroment and software/hardware specs. That is why I performed them 10 times and then I took the average value. Here are the values of my performance tests. Note that both <strong>for</strong> and <strong>while</strong> perform 10,000,000 iterations.</p>
<p><iframe width='576' height='263' frameborder='0' src='https://docs.google.com/spreadsheet/pub?hl=en_US&#038;hl=en_US&#038;key=0Avxdu4aY4-UGdDktZEJHYUh5RWFLd1prOG52dDdTdUE&#038;single=true&#038;gid=0&#038;output=html&#038;widget=true'></iframe></p>
<h2>And the Winner Is</h2>
<p><strong>While</strong> is the winner with an average result of 83.5 milliseconds, while "for" result is 88 average milliseconds.</p>
<p>As the diagram bellow shows, <strong>the while loop is slightly faster</strong>. However we should be aware that these performance gains are significant for large number of iterations!</p>
<figure id="attachment_2668" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/chart_1-1.png"><img src="/wp-content/uploads/2012/01/chart_1-1.png" alt="JavaScript Performance: for vs. while" title="JavaScript Performance: for vs. while" width="600" height="371" class="size-full wp-image-2668" srcset="/wp-content/uploads/2012/01/chart_1-1.png 600w, /wp-content/uploads/2012/01/chart_1-1-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text">JavaScript Performance: for vs. while</figcaption></figure>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/02/02/firebugs-console-time-accuracy/" rel="bookmark" title="Firebug&#8217;s console.time() accuracy">Firebug&#8217;s console.time() accuracy </a></li>
<li><a href="/2010/02/02/profiling-javascript-with-firebug-console-profile-console-time/" rel="bookmark" title="Profiling JavaScript with Firebug. console.profile() &#038; console.time()!">Profiling JavaScript with Firebug. console.profile() &#038; console.time()! </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/06/02/detecting-pressed-key-with-e-which-in-javascript/" rel="bookmark" title="Detecting Pressed Key with e.which in JavaScript">Detecting Pressed Key with e.which in JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/01/24/javascript-performance-for-vs-while/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Does JavaScript undefined Equals undefined?</title>
		<link>/2011/10/21/does-javascript-undefined-equals-undefined/</link>
		<comments>/2011/10/21/does-javascript-undefined-equals-undefined/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 11:36:01 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[typeof]]></category>
		<category><![CDATA[undefined]]></category>

		<guid isPermaLink="false">/?p=2416</guid>
		<description><![CDATA[Weird JS As you know sometimes JavaScript can be weird. Let&#8217;s see the following example and let&#8217;s try to answer the question: does &#8220;undefined&#8221; equals &#8220;undefined&#8221;. What do I mean? First take a look at the following code. var a; var b = undefined; // alerts "false" alert(b == a); Both a and b are &#8230; <a href="/2011/10/21/does-javascript-undefined-equals-undefined/" class="more-link">Continue reading <span class="screen-reader-text">Does JavaScript undefined Equals undefined?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/02/12/how-to-detect-a-variable-existence-in-javascript/" rel="bookmark" title="How to detect a variable existence in JavaScript?">How to detect a variable existence in JavaScript? </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Weird JS</h2>
<figure id="attachment_2419" style="width: 480px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/10/question.mark_.jpg"><img src="/wp-content/uploads/2011/10/question.mark_.jpg" alt="Does &quot;undefined&quot; equals &quot;undefined&quot;?" title="question.mark" width="480" height="624" class="size-full wp-image-2419" srcset="/wp-content/uploads/2011/10/question.mark_.jpg 480w, /wp-content/uploads/2011/10/question.mark_-230x300.jpg 230w" sizes="(max-width: 480px) 100vw, 480px" /></a><figcaption class="wp-caption-text">Does JavaScript &quot;undefined&quot; equals &quot;undefined&quot;?</figcaption></figure>
<p>As you know sometimes JavaScript can be weird. Let&#8217;s see the following example and let&#8217;s try to answer the question: does &#8220;undefined&#8221; equals &#8220;undefined&#8221;. What do I mean?</p>
<p>First take a look at the following code.</p>
<pre lang="javascript">
var a;
var b = undefined;

// alerts "false"
alert(b == a);
</pre>
<p>Both <strong>a</strong> and <strong>b</strong> are <strong>undefined</strong>, but they are <strong>NOT</strong> equal. Now let&#8217;s see where it can become a problem.</p>
<p>We have an object with one member variable that is not defined.</p>
<pre lang="javascript">
var f1 = function()
{
	this.myvar;
};

var obj1 = new f1();
</pre>
<p>Now you&#8217;d like to know whether the object &#8220;b&#8221; has the property &#8220;myvar&#8221;. There are lots of examples online, but what&#8217;s the right way?<br />
<span id="more-2416"></span></p>
<h2>Wrong</h2>
<p>Appearantly the value of b.myvar should be undefined.</p>
<pre lang="javascript">
alert(obj1.myvar); // undefined
</pre>
<p>But that doesn&#8217;t mean that it is the same value if the object was declared this way:</p>
<pre lang="javascript">
var f2 = function()
{
	this.myvar = undefined;
};

var obj2 = new f2();
</pre>
<p>This is a completely different thing. Why? In the first case if we try to check whether the property myvar belongs to obj1 we can go wrong with:</p>
<pre lang="javascript">
if (obj1.myvar) // false
</pre>
<p>This is false simply because obj1.myvar is undefined</p>
<pre lang="javascript">
if (obj2.myvar) // false
</pre>
<p>Again is false, because obj2.myvar is undefined.</p>
<p>The same thing happen when using the other &#8220;famous&#8221; approach.</p>
<pre lang="javascript">
if (typeof obj1['myvar'] != 'undefined') // false
if (typeof obj2['myvar'] != 'undefined') // false
</pre>
<p>Clearly this is not the right answer, becase even it is undefined <strong>myvar </strong>belongs to <strong>obj2</strong>!</p>
<h2>Right</h2>
<p>Now let&#8217;s try with <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="hasOwnProperty on MDN" target="_blank">hasOwnProperty</a> supported by almost every modern browser.</p>
<pre lang="javascript">
if (obj1.hasOwnProperty('myvar')); // false
if (obj2.hasOwnProperty('myvar')); // true
</pre>
<p>Here you can see that when the member variable is explicitly defined as &#8220;undefined&#8221; the object &#8220;has the property&#8221; and hasOwnProperty returns true. Yet again, &#8220;undefined&#8221; does not always equal to &#8220;undefined&#8221;.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/02/12/how-to-detect-a-variable-existence-in-javascript/" rel="bookmark" title="How to detect a variable existence in JavaScript?">How to detect a variable existence in JavaScript? </a></li>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/21/does-javascript-undefined-equals-undefined/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>OOP JavaScript: Accessing Public Methods in Private Methods</title>
		<link>/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/</link>
		<comments>/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 12:05:42 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Accessing Private]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Subroutine]]></category>

		<guid isPermaLink="false">/?p=2339</guid>
		<description><![CDATA[As you know in JavaScript when you define a variable with the special word &#8220;var&#8221; the scope of this variable is within the function. So when you simply wite &#8220;var a = 5&#8221; the variable named &#8220;a&#8221; has a global scope and can be accessed in any function in the global scope. var a = &#8230; <a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" class="more-link">Continue reading <span class="screen-reader-text">OOP JavaScript: Accessing Public Methods in Private Methods</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/03/09/what-make-javascript-closures-work/" rel="bookmark" title="What make JavaScript closures work?">What make JavaScript closures work? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>As you know in JavaScript when you define a variable with the special word &#8220;var&#8221; the scope of this variable is within the function. So when you simply wite &#8220;var a = 5&#8221; the variable named &#8220;a&#8221; has a global scope and can be accessed in any function in the global scope. </p>
<pre lang="javascript">
var a = 5;

function f() { return a; } // returns 5
</pre>
<p>Thus f will return the value of &#8220;a&#8221; which equals to 5. You can also change the value of the global variable in the function body.</p>
<pre lang="javascript">
var a = 5;
function f() { a = 10; return a; }
console.log(a); // equals to 10
</pre>
<p>Now after we call the function f the value of &#8220;a&#8221; will equal to 10. This is because we reference the global variable &#8220;a&#8221; into the function body without using the keyword &#8220;var&#8221;. This means that if you put the &#8220;var&#8221; keyword the variable &#8220;a&#8221; inside the function body is no longer the same variable as the variable defined outside the body. It becames &#8220;local&#8221; and it&#8217;s visible only inside the function.<br />
<span id="more-2339"></span></p>
<h2>Objects &#038; JavaScript</h2>
<p>Actually in JavaScript functions can be also objects. Any variable defined with the &#8220;var&#8221; keyword becomes private, because it&#8217;s only visible inside the function body, while by using the &#8220;this&#8221; keyword we can define global variables visible outside the function body. Let&#8217;s see na example.</p>
<pre lang="javascript">
var f = function() 
{
	var a = 10;
	this.b = 5;
}

var myfunc = new f();
myfunc.a; // is undefined because a is private
myfunc.b; // equals to 5 because b is public
</pre>
<h2>Public and Private Methods in JavaScript</h2>
<p>As you may know in JS you can define functions inside other functions. Actually this is how you can define classes in JavaScript.</p>
<pre lang="javascript">
var myClass = function() 
{
	var f = function() {
		return 10;
	}
}
</pre>
<p>But this in fact defines a local (private) function into myClass and we cannot access it from the outside world. Here&#8217;s a fully functional class with one public and one private method.</p>
<pre lang="javascript">
var myClass = function()
{
	// public method
	this.a = function() {
		return 10;
	}
	
	// private method
	var b = function() {
		return 5;
	}
}

var c = new myClass();
c.a(); // will return 10
c.b(); // is undefined, because is private
</pre>
<h2>Accessing Private Methods from Public Methods</h2>
<p>Easily we can access private methods from public ones.</p>
<pre lang="javascript">
var myClass = function()
{
	// public method
	this.a = function() {
		return b();
	}
	
	// private method
	var b = function() {
		return 5;
	}
}
</pre>
<p>However accessing public mtehods in private ones is more difficult. That&#8217;s because we cannot use &#8220;this&#8221; into the private methods, just because &#8220;this&#8221; refers to the private method<br />
itself and not to the global method, which is &#8220;myClass&#8221; in our case.</p>
<pre lang="javascript">
var myClass = function()
{
	// public method
	this.a = function() {
		return 10;
	}
	
	// private method
	var b = function() {
		return this.a(); // this will result in an error
	}
}
</pre>
<h2>Accessing Public Methods from Private Methods</h2>
<p>To access public methods in private methods you need to define a variable that points to the global &#8220;this&#8221; object.</p>
<pre lang="javascript">
var myClass = function()
{
	var self = this; 
	
	// public method
	this.a = function() {
		return 10;
	}
	
	// private method
	var b = function() {
		return self.a(); // this will return 10
	}
}
</pre>
<p>This variable gives you the bridge between private methods and global &#8220;this&#8221; pointer.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/05/30/object-oriented-javascript-inheritance/" rel="bookmark" title="Object Oriented JavaScript: Inheritance">Object Oriented JavaScript: Inheritance </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/03/09/what-make-javascript-closures-work/" rel="bookmark" title="What make JavaScript closures work?">What make JavaScript closures work? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>A JavaScript Trick You Should Know</title>
		<link>/2011/07/12/a-javascript-trick-you-should-know/</link>
		<comments>/2011/07/12/a-javascript-trick-you-should-know/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 14:49:46 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Brace notation]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Concatenation]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Formal languages]]></category>
		<category><![CDATA[heredoc]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[JavaScript syntax]]></category>
		<category><![CDATA[js solution]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">/?p=2333</guid>
		<description><![CDATA[JavaScript Strings You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this: var str = 'hello world'; This will result in an error, which can be a problem when you deal with large strings. Imagine a string containing some HTML that should be injected via innerHTML. Now &#8230; <a href="/2011/07/12/a-javascript-trick-you-should-know/" class="more-link">Continue reading <span class="screen-reader-text">A JavaScript Trick You Should Know</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/08/26/javascript-flexibility-regex-match/" rel="bookmark" title="JavaScript Flexibility &#8211; Regex match()">JavaScript Flexibility &#8211; Regex match() </a></li>
<li><a href="/2010/08/24/flexible-javascript-splitting-strings/" rel="bookmark" title="Flexible JavaScript &#8211; Splitting Strings">Flexible JavaScript &#8211; Splitting Strings </a></li>
<li><a href="/2010/08/25/flexible-javascript-replace-in-a-string/" rel="bookmark" title="Flexible JavaScript &#8211; Replace in a String">Flexible JavaScript &#8211; Replace in a String </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>JavaScript Strings</h2>
<p>You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this:</p>
<pre lang="javascript">
var str = 'hello
world';
</pre>
<p>This will result in an error, which can be a problem when you deal with large strings. Imagine a string containing some HTML that should be injected via innerHTML. Now one possible solution is to concatenate two or more strings, by splitting the initial string.</p>
<pre lang="javascript">
var str = 'hello'
        + 'world';
</pre>
<p>Howerver this solution result in some additional operations like concatenation. It can be very nice if there was something like the PHP multiline string format.</p>
<h2>The PHP Example</h2>
<p>In PHP you have the same use case with strings. You can have a string concatenated over multiple lines, but you cannot split lines like so:</p>
<pre lang="php">
// the following line is wrong
$str = 'hello
world'; 

// a possible solution to the line above is
$str = 'hello'
     . 'world';
// which is very similar to the js solution in the second example

// ... but in PHP there is the so called HEREDOC
$str = <<<EOD
hello
world
EOD;
</pre>
<p>The heredoc gives us the power to write multiline strings. </p>
<h2>The JavaScript Trick</h2>
<p>Actually in JavaScript there is something that's exactly what Heredoc is meant to be for PHP. Take a look at the last example:</p>
<pre lang="javascript">
var text = <>
this <br />
is
my
multi-line
text
</>.toString();

document.body.innerHTML = text;
</pre>
<p>Thus you can write multiline strings in the JavaScript code.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/08/26/javascript-flexibility-regex-match/" rel="bookmark" title="JavaScript Flexibility &#8211; Regex match()">JavaScript Flexibility &#8211; Regex match() </a></li>
<li><a href="/2010/08/24/flexible-javascript-splitting-strings/" rel="bookmark" title="Flexible JavaScript &#8211; Splitting Strings">Flexible JavaScript &#8211; Splitting Strings </a></li>
<li><a href="/2010/08/25/flexible-javascript-replace-in-a-string/" rel="bookmark" title="Flexible JavaScript &#8211; Replace in a String">Flexible JavaScript &#8211; Replace in a String </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/07/12/a-javascript-trick-you-should-know/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript: Inheritance</title>
		<link>/2011/05/30/object-oriented-javascript-inheritance/</link>
		<comments>/2011/05/30/object-oriented-javascript-inheritance/#comments</comments>
		<pubDate>Mon, 30 May 2011 09:43:53 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[code sample]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Literal]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[oop javascript]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">/?p=2314</guid>
		<description><![CDATA[Objects and JavaScript JavaScript, being a functional language, differs from most of the procedural/object oriented languages we know. The object oriented approach in JavaScript is rather strange. However there is much power in making objects! The syntax is really odd and there are several approaches. Literal Notation As many of you may know the most &#8230; <a href="/2011/05/30/object-oriented-javascript-inheritance/" class="more-link">Continue reading <span class="screen-reader-text">Object Oriented JavaScript: Inheritance</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/03/02/javascript-inheritance-example/" rel="bookmark" title="JavaScript inheritance example">JavaScript inheritance example </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Objects and JavaScript</h2>
<p><a href="/tag/javascript/" title="JavaScript on Stoimen.com">JavaScript</a>, being a functional language, differs from most of the procedural/object oriented languages we know. The object oriented approach in JavaScript is rather strange. However there is much power in making objects! The syntax is really odd and there are several approaches.</p>
<h2>Literal Notation</h2>
<p>As many of you may know the most used notation is the JSON (JavaScript Object Notation).</p>
<pre lang="javascript">
{ 'key1' : 'val1'
, 'key2' : 'val2'
, 'key3' : 'val3'
}
</pre>
<p>Of course this is the very basic example. You can use as value any JavaScript object &#8211; another similar object or a function.</p>
<pre lang="javascript">
{ 'key1' : 'val1'
, 'key2' : { 'inner_key1' : 'inner_val1' }
, 'key3' : function() {
			return 10 + 5;
		 }
}
</pre>
<p>The two examples above are showing an anonymous object in JavaScript, but you can assign this code to some variable.</p>
<pre lang="javascript">
var myObject = 
	{ 'key1' : 'val1'
	, 'key2' : 'val2'
	, 'key3' : 'val3'
	}
</pre>
<p>or </p>
<pre lang="javascript">
var myObject =
	{ 'key1' : 'val1'
	, 'key2' : { 'inner_key1' : 'inner_val1' }
	, 'key3' : function() {
				return 10 + 5;
			 }
	}
</pre>
<p>and then you can call the properties of these objects with the &#8216;.&#8217; operator:</p>
<pre lang="javascript">
myObject.key1;
myObject.key2.inner_key1;
myObject.key3();
</pre>
<p>So far so good &#8211; this is the literal object notation in JavaScript. However there is another &#8220;objects&#8221; in JavaScript.<br />
<span id="more-2314"></span></p>
<h2>Objects and Functions in JavaScript</h2>
<p>Actually in JS the functions are also objects (classes). So you can define a function and then define another function inside or use the &#8220;this&#8221; operator.</p>
<pre lang="javascript">
var a = function(name)
{
	this.name = name;
	
	this.print = function() {
		alert(this.name);	
	}
}
</pre>
<p>Then you can make a new instance of this &#8220;class&#8221;.</p>
<pre lang="javascript">
var myObject = new a('hello world');
var anotherObject = new a('some test');

myObject.print(); // will print the string "hello world"
anotherObject.print(); // will print the string "some test"
</pre>
<p>Thus the function <strong>&#8220;a&#8221;</strong> defined within the code <strong>&#8220;var a = function(name) &#8230;&#8221;</strong> is a class in JavaScript. You can make instances (objects) of this class with the &#8220;new&#8221; operator.</p>
<p>You can use another notation for defining the function &#8220;a&#8221;, so both can be used and it&#8217;s up to you to decide which one is more convenient.</p>
<pre lang="javascript">
function a(name) 
{
	this.name = name;
	
	this.print = function() {
		alert(this.name);	
	}	
}
</pre>
<h2>Prototypes</h2>
<p>JS gives you more power. In most of the languages you&#8217;ve to define the class first and then make objects. In JS you can define a &#8220;class&#8221; then make some objects and then add some properties to the class by using the prototype.</p>
<pre lang="javascript">
var a = function(name) 
{
	this.name = name;
}

var myObj = new a('hello world');

a.prototype.print = function() {
	alert(this.name);	
}

myObj.print();
</pre>
<p>So you can see how you can add functions and properties to an already defined class using the prototype. Thus you can call the &#8220;print&#8221; function on the myObj object.</p>
<h2>Inheritance</h2>
<p>The prototype built-in property can be used to make use of one of the main OOP features &#8211; inheritance. </p>
<pre lang="javascript">
var SuperClass = function()
{
	this.superFunction = function() {
		return 'function in Super Class';	
	}
}

var sup = new SuperClass();
sup.superFunction(); // will return the string "function in Super Class"
</pre>
<p>Now you can add one more class and set its prototype to the SuperClass</p>
<pre lang="javascript">
var SubClass = function()
{
	this.subFunction = function() {
		return 'function in Sub Class';	
	}	
}

SubClass.prototype = new SuperClass();
</pre>
<p>Thus you make SubClass inherit from SuperClass. Now you can call the SuperClass properties from any SubClass object.</p>
<pre lang="javascript">
var sub = new SubClass();
sub.superFunction(); // will return the string "function in Super Class"
sub.subFunction(); // will return the string "function in Sub Class"
</pre>
<p>Even more! You can add properties to the super class with prototype.</p>
<pre lang="javascript">
SuperClass.prototype.myFunc = function() {
	return 'my new function';	
}
</pre>
<p>And then call it from the sub class:</p>
<pre lang="javascript">
sub.myFunc(); // will return the string "my new function"
</pre>
<h2>Override a Function</h2>
<p>What happens if you have the same function name in both classes. Actually this overrides the method in the sub class.</p>
<pre lang="javascript">
var SuperClass = function()
{
	this.myFunc = function() {
		return 'function in Super Class';	
	}
}

var SubClass = function()
{
	this.myFunc = function() {
		return 'function in Sub Class';	
	}	
}

SubClass.prototype = new SuperClass();

var sub = new SubClass();
sub.myFunc(); // will return the string "function in Sub Class"
</pre>
<p>while </p>
<pre lang="javascript">
var sup = new SuperClass();
sup.myFunc(); // will return the string "function in Super Class"
</pre>
<h2>Conclusion</h2>
<p>Now you know there are classes, inheritance and overrides in JavaScript &#8211; thus you can improve your object oriented JavaScript!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/" rel="bookmark" title="OOP JavaScript: Accessing Public Methods in Private Methods">OOP JavaScript: Accessing Public Methods in Private Methods </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/08/11/quick-look-at-javascript-objects/" rel="bookmark" title="Quick Look at JavaScript Objects">Quick Look at JavaScript Objects </a></li>
<li><a href="/2010/03/02/javascript-inheritance-example/" rel="bookmark" title="JavaScript inheritance example">JavaScript inheritance example </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/05/30/object-oriented-javascript-inheritance/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Adding a Custom Button to TinyMCE</title>
		<link>/2011/02/16/adding-a-custom-button-to-tinymce/</link>
		<comments>/2011/02/16/adding-a-custom-button-to-tinymce/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 08:31:04 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[CKEditor]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[FCKEditor]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[online based editor]]></category>
		<category><![CDATA[opinion]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[TinyMCE]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WYSIWYG]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">/?p=2176</guid>
		<description><![CDATA[TinyMCE First thing to say TinyMCE is a very popular WYSIWYG online based editor. It&#8217;s very widely used in the web, as may already know it it&#8217;s part of the default WordPress installation. Out of the web, of course, there are some other editors as well. The most used and well developed projects are the &#8230; <a href="/2011/02/16/adding-a-custom-button-to-tinymce/" class="more-link">Continue reading <span class="screen-reader-text">Adding a Custom Button to TinyMCE</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/02/17/adding-a-custom-button-to-tinymce-revised/" rel="bookmark" title="Adding a Custom Button to TinyMCE &#8211; REVISED">Adding a Custom Button to TinyMCE &#8211; REVISED </a></li>
<li><a href="/2010/01/31/speed-up-the-javascript-it-can-change-dramatically-the-user-experience/" rel="bookmark" title="Speed up the JavaScript. It can change dramatically the user experience.">Speed up the JavaScript. It can change dramatically the user experience. </a></li>
<li><a href="/2010/02/11/javascript-optimization-lazy-loading/" rel="bookmark" title="JavaScript optimization. Lazy loading.">JavaScript optimization. Lazy loading. </a></li>
<li><a href="/2011/04/05/jquery-unbind/" rel="bookmark" title="jQuery.unbind()">jQuery.unbind() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>TinyMCE</h2>
<p>First thing to say <a title="TinyMCE Homepage" href="http://tinymce.moxiecode.com/" target="_blank">TinyMCE</a> is a very popular <a title="WYSIWYG Wikipedia page" href="http://en.wikipedia.org/wiki/WYSIWYG" target="_blank">WYSIWYG</a> online based editor. It&#8217;s very widely used in the web, as may already know it it&#8217;s part of the default WordPress installation. Out of the web, of course, there are some other editors as well. The most used and well developed projects are the <a title="YUI 2 Rich Text Editor Homepage" href="http://developer.yahoo.com/yui/editor/" target="_blank">Yahoo!&#8217;s YUI 2 Rich Text Editor</a> and <a title="CKEditor Homepage" href="http://ckeditor.com/" target="_blank">CKEditor</a> also known with his past name &#8211; FCKEditor.<a href="/wp-content/uploads/2011/02/TinyMCE_Full_Featured.png"><img class="aligncenter size-full wp-image-2206" title="TinyMCE_Full_Featured" src="/wp-content/uploads/2011/02/TinyMCE_Full_Featured.png" alt="TinyMCE Full Featured Example" width="640" height="528" srcset="/wp-content/uploads/2011/02/TinyMCE_Full_Featured.png 640w, /wp-content/uploads/2011/02/TinyMCE_Full_Featured-300x247.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></a></p>
<p>Before I proceed with this post, let me say that I&#8217;m working and this tutorial is based on <strong>version 3.1.1</strong> released on <strong>18 Aug 2008</strong>.<span id="more-2176"></span></p>
<h2>Including TinyMCE</h2>
<p>The only thing you&#8217;ve to do is to include a TinyMCE javascript file and write a simple init. The examples of installing it come with the compressed version of the javascript file:</p>
<pre lang="html4strict">
<script src="/scripts/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script>
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	force_br_newlines : true,
	theme_advanced_buttons1 : "link,unlink,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_buttons4 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "",
	theme_advanced_resizing : true
});
</script>
</pre>
<p>The result of this code is shown on the picture:</p>
<p><a href="/wp-content/uploads/2011/02/TinyMCE_Before.png"><img class="aligncenter size-full wp-image-2207" title="TinyMCE_Before" src="/wp-content/uploads/2011/02/TinyMCE_Before.png" alt="TinyMCE Before" width="653" height="313" srcset="/wp-content/uploads/2011/02/TinyMCE_Before.png 653w, /wp-content/uploads/2011/02/TinyMCE_Before-300x143.png 300w" sizes="(max-width: 653px) 100vw, 653px" /></a></p>
<p>However for this tutorial you&#8217;ve to include the uncompressed file, because the compressed one is impossible to read and modify. At the end of all this you can compress yet again the resulting file with some js compressor as <a title="YUI Compressor Homepage" href="http://developer.yahoo.com/yui/compressor/" target="_blank">YUI Compressor</a>, <a title="JSMin" href="http://www.crockford.com/javascript/jsmin.html" target="_blank">JSMin</a> or <a title="Google Closure Compiler Homepage" href="http://code.google.com/closure/compiler/" target="_blank">Google Closure Compiler</a>. So instead of including the file above, you&#8217;ve to include the _src version as it is:</p>
<pre lang="html4strict">
<script src="/scripts/tinymce/jscripts/tiny_mce/tiny_mce_src.js"></script>
</pre>
<h2>Adding the Button</h2>
<p>Now there is a small configuration object passed to the init function. First you&#8217;ve to modify it a bit:</p>
<pre lang="javascript">
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	force_br_newlines : true,
	theme_advanced_buttons1 : "link,unlink,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,mybutton",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_buttons4 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "",
	theme_advanced_resizing : true
});
</pre>
<p>Usually in the /tinymce/jscripts/tiny_mce/themes/advanced/editor_template_src.js file there are some buttons described:</p>
<pre lang="javascript">
tinymce.create('tinymce.themes.AdvancedTheme', {
	// Control name lookup, format: title, command
	controls : {
		bold : ['bold_desc', 'Bold'],
		italic : ['italic_desc', 'Italic'],
		underline : ['underline_desc', 'Underline'],
		strikethrough : ['striketrough_desc', 'Strikethrough'],
		justifyleft : ['justifyleft_desc', 'JustifyLeft'],
		justifycenter : ['justifycenter_desc', 'JustifyCenter'],
		justifyright : ['justifyright_desc', 'JustifyRight'],
		justifyfull : ['justifyfull_desc', 'JustifyFull'],
		bullist : ['bullist_desc', 'InsertUnorderedList'],
		numlist : ['numlist_desc', 'InsertOrderedList'],
		outdent : ['outdent_desc', 'Outdent'],
		indent : ['indent_desc', 'Indent'],
...
</pre>
<p>What you need to do is simply to add a new line to this config array:</p>
<pre lang="javascript">
...
        blockquote : ['blockquote_desc', 'mceBlockQuote'],
	mybutton : ['mybutton_desc','myFunc']
},
</pre>
<p>That&#8217;s not enough of course! Now TinyMCE sees this definition, but nothing happens. It cannot build the new button yet. What&#8217;s next?</p>
<h2>Styling the Button</h2>
<p>After giving the new button a name, TinyMCE constructs a markup with the given name into the controlbar:</p>
<p><a href="/wp-content/uploads/2011/02/TinyMCE_Button_Step-_1.png"><img class="aligncenter size-full wp-image-2208" title="TinyMCE_Button_Step _1" src="/wp-content/uploads/2011/02/TinyMCE_Button_Step-_1.png" alt="TinyMCE Button Step 1" width="654" height="315" srcset="/wp-content/uploads/2011/02/TinyMCE_Button_Step-_1.png 654w, /wp-content/uploads/2011/02/TinyMCE_Button_Step-_1-300x144.png 300w" sizes="(max-width: 654px) 100vw, 654px" /></a></p>
<pre lang="html4strict">
<td>
	<a title="advanced.mybutton_desc" onclick="return false;" onmousedown="return false;" class="mceButton mceButtonEnabled mce_mybutton" href="javascript:;" id="mce_0_mybutton">
		<span class="mceIcon mce_mybutton"></span>
	</a>
</td>
</pre>
<p>This name can be simply used in the CSS file (/tinymce/jscripts/tiny_mce/themes/advanced/skins/default/ui.css) to style the button. Because TinyMCE uses sprite for its background images, you can copy/paste the style from some other button and than adjust the background position, only after you&#8217;ve added the new icon to the sprite. Here&#8217;s simply a copy of the style from another button:</p>
<pre lang="css">
...
.defaultSkin span.mce_numlist {background-position:-80px 0}
.defaultSkin span.mce_mybutton {background-position:-460px 0}
.defaultSkin span.mce_justifyleft {background-position:-460px 0}
...
</pre>
<p>You can see how the button appears in the control bar!</p>
<p><a href="/wp-content/uploads/2011/02/TinyMCE_Button_Step_2.png"><img class="aligncenter size-full wp-image-2209" title="TinyMCE_Button_Step_2" src="/wp-content/uploads/2011/02/TinyMCE_Button_Step_2.png" alt="TinyMCE Button Step 2" width="655" height="313" srcset="/wp-content/uploads/2011/02/TinyMCE_Button_Step_2.png 655w, /wp-content/uploads/2011/02/TinyMCE_Button_Step_2-300x143.png 300w" sizes="(max-width: 655px) 100vw, 655px" /></a></p>
<h2>Binding the Button</h2>
<p>The final job to do is to bind the button with some action. This thing happens again in /tinymce/jscripts/tiny_mce/themes/advanced/editor_template_src.js. You just have to search for other bindings &#8211; let&#8217;s say mceCleanup. There you can write your new function &#8211; myFunc:</p>
<pre lang="javascript">
FormatBlock : function(ui, val) {
	...
},

myFunc : function() {
	this.editor.selection.setContent('Hello World!');
},

mceCleanup : function() {
	...
},
</pre>
<p>Now after clicking the button the method will be executed. <a href="/wp-content/uploads/2011/02/TinyMCE_Button_Step_3.png"><img class="aligncenter size-full wp-image-2210" title="TinyMCE_Button_Step_3" src="/wp-content/uploads/2011/02/TinyMCE_Button_Step_3.png" alt="TinyMCE Button Step 3" width="653" height="312" srcset="/wp-content/uploads/2011/02/TinyMCE_Button_Step_3.png 653w, /wp-content/uploads/2011/02/TinyMCE_Button_Step_3-300x143.png 300w" sizes="(max-width: 653px) 100vw, 653px" /></a></p>
<p>Note that the name of the method should be exactly the same as described in the controls array in /tinymce/jscripts/tiny_mce/themes/advanced/editor_template_src.js. If not the button wont work ever.</p>
<h2>Final Steps</h2>
<p>Now you&#8217;ve to compress (if you wish) the js files and deploy your editor.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/02/17/adding-a-custom-button-to-tinymce-revised/" rel="bookmark" title="Adding a Custom Button to TinyMCE &#8211; REVISED">Adding a Custom Button to TinyMCE &#8211; REVISED </a></li>
<li><a href="/2010/01/31/speed-up-the-javascript-it-can-change-dramatically-the-user-experience/" rel="bookmark" title="Speed up the JavaScript. It can change dramatically the user experience.">Speed up the JavaScript. It can change dramatically the user experience. </a></li>
<li><a href="/2010/02/11/javascript-optimization-lazy-loading/" rel="bookmark" title="JavaScript optimization. Lazy loading.">JavaScript optimization. Lazy loading. </a></li>
<li><a href="/2011/04/05/jquery-unbind/" rel="bookmark" title="jQuery.unbind()">jQuery.unbind() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/02/16/adding-a-custom-button-to-tinymce/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Diving into Node.js &#8211; A Long Polling Example</title>
		<link>/2010/12/02/diving-into-node-js-a-long-polling-example/</link>
		<comments>/2010/12/02/diving-into-node-js-a-long-polling-example/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 09:51:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[chat-like applications]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[Internet protocols]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[long polling server]]></category>
		<category><![CDATA[Node]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[real time data]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[typical web server]]></category>
		<category><![CDATA[web developers]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[web servers]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">/?p=2041</guid>
		<description><![CDATA[Node.js vs. The World What is typical for most of the web servers is that they listen for requests and respond as quickly as possible on every one of them. In fact the speed of the response is one of the main targets of optimization for developers. Fast servers are what everyone needs. From web &#8230; <a href="/2010/12/02/diving-into-node-js-a-long-polling-example/" class="more-link">Continue reading <span class="screen-reader-text">Diving into Node.js &#8211; A Long Polling Example</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/11/19/diving-into-node-js-very-first-app/" rel="bookmark" title="Diving into Node.js &#8211; Very First App">Diving into Node.js &#8211; Very First App </a></li>
<li><a href="/2010/11/16/diving-into-node-js-introduction-and-installation/" rel="bookmark" title="Diving into Node.js &#8211; Introduction &#038; Installation">Diving into Node.js &#8211; Introduction &#038; Installation </a></li>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Node.js vs. The World</h2>
<p><a href="/wp-content/uploads/2010/11/node-js.jpg"><img class="aligncenter size-full wp-image-2088" title="node js" src="/wp-content/uploads/2010/11/node-js.jpg" alt="" width="445" height="193" srcset="/wp-content/uploads/2010/11/node-js.jpg 445w, /wp-content/uploads/2010/11/node-js-300x130.jpg 300w" sizes="(max-width: 445px) 100vw, 445px" /></a></p>
<p>What is typical for most of the web servers is that they listen for requests and respond as quickly as possible on every one of them. In fact the speed of the response is one of the main targets of optimization for developers. Fast servers are what everyone needs. From web developers to website visitors!</p>
<p>In the field of the that battle different web servers have different &#8220;weapons&#8221; to gain time. While this is useful in most of the cases, when it comes to a chat-like applications and <a title="Node.js" href="http://nodejs.org/" target="_blank">Node.js</a> approaches, the response is not always immediately returned. As I described in my <a title="Diving into Node.js – Very First App" href="/2010/11/19/diving-into-node-js-very-first-app/" target="_self">posts</a> until now about Node.js, a simple web server may wait for an event to be emitted, and than return the response.<span id="more-2041"></span></p>
<p>I wrote about <a title="Diving into Node.js – Very First App" href="/2010/11/19/diving-into-node-js-very-first-app/" target="_self">how to write the very first server</a>, but than I didn&#8217;t described how to make a &#8220;non-responding&#8221; server. By the term &#8220;non-responding&#8221; I mean a server that responds not immediately after it has received and parsed/executed the request.</p>
<p>A typical web server, responding immediately on every request, written with Node, this may look like so:</p>
<pre lang="javascript">var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
</pre>
<p>The code that shows us that the request is executed and responds in these lines:</p>
<pre lang="javascript">  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
</pre>
<p>Actually by commenting/removing those lines you&#8217;ll get a server that simply doesn&#8217;t respond &#8211; ever! This of course is not the main goal, but you can start from somewhere.</p>
<pre lang="javascript">var http = require('http');
http.createServer(function (req, res) {
  // res.writeHead(200, {'Content-Type': 'text/plain'});
  // res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
</pre>
<p>This is how you can hold for a while. Perhaps putting these lines in a conditional statement and periodically checking for some event to occur will do the job.</p>
<pre lang="javascript">var http = require('http');
http.createServer(function (req, res) {
  if (something) {
     res.writeHead(200, {'Content-Type': 'text/plain'});
     res.end('Hello World\n');
  }
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
</pre>
<h2>Looping Server</h2>
<p>As described in the code above if you put the respond into a conditional you can possibly return the response after some event has fired. The only thing is to periodically loop and check for this condition to be true. Assuming your server&#8217;s code is in server.js, started with the &#8220;node server.js&#8221; command, you&#8217;ve to put this code into it:</p>
<pre lang="javascript" escaped="true">var http = require("http"),
    fs	 = require("fs");  

// we create a server with automatically binding
// to a server request listener
http.createServer(function(request, response) {
	checkFile(request, response);
}).listen(8124);

function checkFile(request, response)
{
	var date = new Date();
	if (date-request.socket._idleStart.getTime() &gt; 59999) {
		response.writeHead(200, {
			'Content-Type'   : 'text/plain',
			'Access-Control-Allow-Origin' : '*'
		});

		// return response
		response.write('OK', 'utf8');
		response.end();
	}

	// we check the information from the file, especially
	// to know when it was changed for the last time
	fs.stat('filepath', function(err, stats) {
		// if the file is changed
		if (stats.mtime.getTime() &gt; request.socket._idleStart.getTime()) {
			// read it
			fs.readFile('filepath', 'utf8', function(err, data) {
				// return the contents
				response.writeHead(200, {
					'Content-Type'   : 'text/plain',
					'Access-Control-Allow-Origin' : '*'
				});

				// return response
				response.write(data, 'utf8');
				response.end();

				// return
				return false;
			});
		}
	});	

	setTimeout(function() { checkFile(request, response) }, 10000);
};
</pre>
<p>Note: you must change &#8220;filepath&#8221; with an existing file path in your system.</p>
<p>Here we can read periodically the file&#8217;s mtime, which is the modify time. Thus whenever the file is changed the server will return the response. It&#8217;s interesting to note that if no change occurs within the timeout period you&#8217;ve to return some status message as we did in those lines:</p>
<pre lang="javascript" escaped="true">        
var date = new Date();
if (date-request.socket._idleStart.getTime() > 59999) {
	response.writeHead(200, {
		'Content-Type'   : 'text/plain',
		'Access-Control-Allow-Origin' : '*'
	});
	
	// return response
	response.write('OK', 'utf8');
	response.end();
}
</pre>
<p>The client side should long poll this server. The example bellow is written in jQuery. The only &#8220;special&#8221; thing is that after receiving the response, you must call the server again.</p>
<h3>The Client</h3>
<p>The client is nothing new to the jQuery/JavaScript community except you must call again the server when the response is returned:</p>
<pre lang="javascript">function callNode() {
    $.ajax({
        cache : false,
		// setup the server address
        url : 'http://www.example.com:8124/',
        data : {},
        success : function(response, code, xhr) {

            if ('OK' == response) {
            	callNode();
                return false;
            }

            // do whatever you want with the response
            ...

            // make new call
            callNode();
        }
    });
};
callNode();
</pre>
<h2>Summary</h2>
<p>As Node can hold the response, the only thing you should do is to check periodically for something as in this example this was a file change. Than it&#8217;s important to change the client so it can call the server after the response from it is received.</p>
<p>Typically Node can be used for chat-like applications or whatever apps that must deliver real time data, but for sure it is really great software.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/11/19/diving-into-node-js-very-first-app/" rel="bookmark" title="Diving into Node.js &#8211; Very First App">Diving into Node.js &#8211; Very First App </a></li>
<li><a href="/2010/11/16/diving-into-node-js-introduction-and-installation/" rel="bookmark" title="Diving into Node.js &#8211; Introduction &#038; Installation">Diving into Node.js &#8211; Introduction &#038; Installation </a></li>
<li><a href="/2010/03/23/read-remote-file-content-type-with-zend_http_client/" rel="bookmark" title="Read Remote File Content-Type with Zend_Http_Client">Read Remote File Content-Type with Zend_Http_Client </a></li>
<li><a href="/2011/04/13/post-with-zend_http_client/" rel="bookmark" title="POST with Zend_Http_Client">POST with Zend_Http_Client </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/12/02/diving-into-node-js-a-long-polling-example/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Diving into Node.js &#8211; Introduction &#038; Installation</title>
		<link>/2010/11/16/diving-into-node-js-introduction-and-installation/</link>
		<comments>/2010/11/16/diving-into-node-js-introduction-and-installation/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 08:17:32 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Apache Corporation]]></category>
		<category><![CDATA[Apache HTTP Server]]></category>
		<category><![CDATA[application/server]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[classical chat server]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[Inter-process communication]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[normal server]]></category>
		<category><![CDATA[normal web server]]></category>
		<category><![CDATA[operating system]]></category>
		<category><![CDATA[Push technology]]></category>
		<category><![CDATA[Server-side JavaScript]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[typical web server]]></category>
		<category><![CDATA[Web browser]]></category>
		<category><![CDATA[web developer team]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[web server example]]></category>
		<category><![CDATA[web serving functionality]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=2037</guid>
		<description><![CDATA[Why I Need Something Like Node.js? First of all the use of some software is needed not because of itself, but because of the need of some specific functionality. In my case this was the need of real time news feed. Of course there is a way to make this without Node.js, as I&#8217;ll describe &#8230; <a href="/2010/11/16/diving-into-node-js-introduction-and-installation/" class="more-link">Continue reading <span class="screen-reader-text">Diving into Node.js &#8211; Introduction &#038; Installation</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/12/02/diving-into-node-js-a-long-polling-example/" rel="bookmark" title="Diving into Node.js &#8211; A Long Polling Example">Diving into Node.js &#8211; A Long Polling Example </a></li>
<li><a href="/2010/11/19/diving-into-node-js-very-first-app/" rel="bookmark" title="Diving into Node.js &#8211; Very First App">Diving into Node.js &#8211; Very First App </a></li>
<li><a href="/2010/01/31/speed-up-the-javascript-it-can-change-dramatically-the-user-experience/" rel="bookmark" title="Speed up the JavaScript. It can change dramatically the user experience.">Speed up the JavaScript. It can change dramatically the user experience. </a></li>
<li><a href="/2010/01/11/what-should-be-optimized-in-one-web-page/" rel="bookmark" title="What should be optimized in one web page?">What should be optimized in one web page? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Why I Need Something Like Node.js?</h2>
<p>First of all the use of some software is needed not because of itself, but because of the need of some specific functionality. In my case this was the need of real time news feed. Of course there is a way to make this without <a title="Node.js" href="http://nodejs.org/" target="_blank">Node.js</a>, as I&#8217;ll describe later in this post, but there are several disadvantages. However to begin from somewhere, let me explain what is Node.js.</p>
<h2>Introducing Node.js</h2>
<p>Perhaps the best way do describe what is Node.js is from its <a title="Node.js" href="http://nodejs.org/#about" target="_blank">about page</a>.</p>
<blockquote><p>Node&#8217;s goal is to provide an easy way to build scalable network programs. In the &#8220;hello world&#8221; web server example above, many client connections can be handled concurrently. Node tells the operating system (through epoll, kqueue, /dev/poll, or select) that it should be notified when a new connection is made, and then it goes to sleep. If someone new connects, then it executes the callback. Each connection is only a small heap allocation.</p>
<p>&#8230;</p></blockquote>
<p>In general Node is a program using the Google Chrome&#8217;s <a title="V8 JavaScript engine" href="http://code.google.com/p/v8/" target="_blank">V8 JavaScript</a> engine, which in turn is a program that can parse and execute code written in JavaScript. V8 is a very very interesting project itself. First of all from Google have developed this engine especially for one of his products &#8211; their browser <a title="Google Chrome" href="http://www.google.com/chrome" target="_blank">Chrome</a>. It pretends to be and by no means is one of the masterpieces of Google. It is fast and reliable engine, written in C++ and JavaScript, as <a title="V8 JavaScript engine on Wikipedia" href="http://en.wikipedia.org/wiki/V8_%28JavaScript_engine%29" target="_blank">Wikipedia&#8217;s page</a> says. Actually this code is open source and can be embedded in whatever application written in C++. Thus you can have in your application a JavaScript engine.<span id="more-2037"></span></p>
<p>Such kind of application/server is Node.js. With V8 embedded into, Node is intended to run on a machine and to serve some requests. Just like a normal web server, but with few differences. First of all Node is not only serving HTTP requests, but also TCP. Well I&#8217;m not so deep into this yet, but so far that works for me. What I need for now is its web serving functionality.</p>
<p>The way Node works, however is different from what we know from every other web server. Here it is a normal server in general:</p>
<p><a href="/wp-content/uploads/2010/11/client-server.png"></a><a href="/wp-content/uploads/2010/11/client-server.png"><img class="alignleft size-full wp-image-2053" title="client-server" src="/wp-content/uploads/2010/11/client-server.png" alt="Client Server" width="600" height="150" srcset="/wp-content/uploads/2010/11/client-server.png 600w, /wp-content/uploads/2010/11/client-server-300x75.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a></p>
<p>Whenever a request comes to the server, it tries to return the response as fast as possible. This is one of the main goals of every web developer team &#8211; to make the application as fast as possible. However in Node the things work different:</p>
<p><a href="/wp-content/uploads/2010/11/node-client-server.png"><img class="alignleft size-full wp-image-2058" title="node-client-server" src="/wp-content/uploads/2010/11/node-client-server.png" alt="Node.js Client Server Application" width="600" height="150" srcset="/wp-content/uploads/2010/11/node-client-server.png 600w, /wp-content/uploads/2010/11/node-client-server-300x75.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a></p>
<p>Here the server doesn&#8217;t return immediately the response, but holds as long as possible until some &#8220;event&#8221; occurs. You can thing of Node as a classical chat server. Until one of the peers doesn&#8217;t write a message, the other doesn&#8217;t receive anything.</p>
<blockquote><p>Some may be confused as how you can use both the typical web server, as Apache, with Node as another web server within the same page. The answer is &#8211; with AJAX. As you know thus you can have only one chunk of your page talking with the server &#8211; in this case the entire page is returned by Apache, but inside the page you can have a chat app using Node as a server, working perhaps on a different port on the same machine.</p></blockquote>
<p>Typically this scenario can be simulated without Node, but as I said, with some disadvantages. Let&#8217;s say you have two browsers and one server &#8211; you can infinitely loop AJAX calls to the server and whenever there is new message on the server return it to the browser &#8211; this is quite inefficient. There are two approaches in this case.</p>
<ol>
<li>You can make those AJAX calls too recently and you can have a call every N seconds. In that case if N is to short you&#8217;ll have too many calls and in case the other peer doesn&#8217;t make anything the server will be unusually loaded with no practical effect.</li>
<li>In the second case you can choose to make N too long &#8211; for instance 20 seconds. This is also bad, because during this period of time the peers may want to interact and chat.</li>
</ol>
<p>In that case Node is a web server, but simply doesn&#8217;t return the response until a specific event doesn&#8217;t occur. How does this look like in a web browser. You may know how looking into the <a title="Firebug" href="https://addons.mozilla.org/en-US/firefox/addon/1843/" target="_blank">Firebug console</a> for every AJAX call you see the request and how many seconds it has taken. If you make an AJAX call to a Node server app the request just doesn&#8217;t respond immediately.</p>
<h2>Installing Node.js</h2>
<p>The process of installation is <a title="Node.js Build" href="http://nodejs.org/#build" target="_blank">described quite well</a>, so I&#8217;m going to describe it in few steps.</p>
<ol>
<li>Download the application from Git, note that V8 is embedded inside. The version while this post was written and of course was stable is <a title="Node.js v0.2.4" href="http://nodejs.org/dist/node-v0.2.4.tar.gz" target="_blank">v0.2.4</a></li>
<li>For those familiar with Unix/Linux systems, there are three simple steps following, however without <a title="Gnu Compiler Collection" href="http://gcc.gnu.org/" target="_blank">GCC (Gnu Compiler Collection)</a> you wont be able to proceed! I&#8217;m writing this especially for the Mac OS users, as they may know there is no built-in GCC there.</li>
<li>./configure</li>
<li>make</li>
<li>make install</li>
<li>make test (optional)</li>
</ol>
<p>This in general is enough. After all that you can write the node command in your terminal. However if there are some problems while compiling it you may refer to Git and the Node community for more help. Don&#8217;t forget that V8 can work only on x86, x64 and ARM architectures.</p>
<h2>Summary</h2>
<p>There are few things that can be summarized now:</p>
<ol>
<li>First of all Node can be a web server, as Apache is, but with the option to respond on event firing.</li>
<li>You can request it either with a web page loaded in your browser or within a chunk of the page via AJAX.</li>
<li>To install Node you need GCC. It comes with V8 JavaScript engine inside, which can run only on x86, x64 and ARM.</li>
</ol>
<p>So far so good! Now we have Node installed, practically useless, because there is no application running on it. In my next post I&#8217;ll describe how to run your first application.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/12/02/diving-into-node-js-a-long-polling-example/" rel="bookmark" title="Diving into Node.js &#8211; A Long Polling Example">Diving into Node.js &#8211; A Long Polling Example </a></li>
<li><a href="/2010/11/19/diving-into-node-js-very-first-app/" rel="bookmark" title="Diving into Node.js &#8211; Very First App">Diving into Node.js &#8211; Very First App </a></li>
<li><a href="/2010/01/31/speed-up-the-javascript-it-can-change-dramatically-the-user-experience/" rel="bookmark" title="Speed up the JavaScript. It can change dramatically the user experience.">Speed up the JavaScript. It can change dramatically the user experience. </a></li>
<li><a href="/2010/01/11/what-should-be-optimized-in-one-web-page/" rel="bookmark" title="What should be optimized in one web page?">What should be optimized in one web page? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/11/16/diving-into-node-js-introduction-and-installation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>2xjQuery: Select a Selector</title>
		<link>/2010/09/24/2xjquery-select-a-selector/</link>
		<comments>/2010/09/24/2xjquery-select-a-selector/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 08:23:58 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Comparison of JavaScript frameworks]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Ext]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery javascript library]]></category>
		<category><![CDATA[jquery selectors]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[openlayers]]></category>
		<category><![CDATA[openlayers library]]></category>
		<category><![CDATA[Span and div]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=1989</guid>
		<description><![CDATA[Selectors in jQuery If you&#8217;re familiar with jQuery you should already know what are selectors and how they work. However we&#8217;re used to get some DOM element with a specific selector, but actually sometimes you cannot select directly what you need. This is especially true when mixing more than one JavaScript libraries. In my case &#8230; <a href="/2010/09/24/2xjquery-select-a-selector/" class="more-link">Continue reading <span class="screen-reader-text">2xjQuery: Select a Selector</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/01/08/jquery-vs-pure-javascript/" rel="bookmark" title="jQuery vs. pure JavaScript">jQuery vs. pure JavaScript </a></li>
<li><a href="/2009/07/29/cancel-bubbling-on-element-click-with-jquery/" rel="bookmark" title="cancel bubbling on element click with jQuery">cancel bubbling on element click with jQuery </a></li>
<li><a href="/2009/11/02/ajax-in-jquery/" rel="bookmark" title="$.ajax in jQuery">$.ajax in jQuery </a></li>
<li><a href="/2009/04/01/jquery-accessing-a-child-element/" rel="bookmark" title="jQuery &#8211; accessing a child element">jQuery &#8211; accessing a child element </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Selectors in jQuery</h2>
<p><a href="/wp-content/uploads/2010/09/jquery.png"><img class="alignleft size-full wp-image-1999" title="jquery" src="/wp-content/uploads/2010/09/jquery.png" alt="jQuery JavaScript Library" width="259" height="65" /></a></p>
<p>If you&#8217;re familiar with <a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a> you should already know what are selectors and how they work. However we&#8217;re used to get some DOM element with a specific selector, but actually sometimes you cannot select directly what you need. This is especially true when mixing more than one JavaScript libraries. In my case <a title="OpenLayers" href="http://openlayers.org/" target="_blank">OpenLayers</a> generated a vector layer and jQuery was supposed to change the vectors fill-color property.</p>
<p><a href="/wp-content/uploads/2010/09/openlayers.png"><img class="alignleft size-full wp-image-2000" title="openlayers" src="/wp-content/uploads/2010/09/openlayers.png" alt="OpenLayers" width="195" height="37" /></a></p>
<p>With the simple $(&#8216;path&#8217;) I easily got all vectors in the current document, but the problem was that I took them as text. Thus it came to me to use nested jQuery &#8211; $($(&#8216;selector&#8217;));</p>
<p>To describe what actually happened in my case, let me show you a breve example.</p>
<h2>Example</h2>
<p>Here&#8217;s a short HTML markup:</p>
<pre lang="html4strict">
<div><span>text text</span></div>

</pre>
<p>With jQuery I can select both the DIV and SPAN with the simple $(&#8216;div&#8217;) and $(&#8216;span&#8217;), but just for the example lets assume I&#8217;ve only the inner text of the DIV tag:</p>
<pre lang="javascript">$('div').html();
</pre>
<p>That was the case in the OpenLayers/jQuery problem. Now I can easily use another jQuery selector:</p>
<pre lang="javascript">$($('div').html());
</pre>
<p>This will return the same as $(&#8216;span&#8217;) &#8211; a jQuery object.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/01/08/jquery-vs-pure-javascript/" rel="bookmark" title="jQuery vs. pure JavaScript">jQuery vs. pure JavaScript </a></li>
<li><a href="/2009/07/29/cancel-bubbling-on-element-click-with-jquery/" rel="bookmark" title="cancel bubbling on element click with jQuery">cancel bubbling on element click with jQuery </a></li>
<li><a href="/2009/11/02/ajax-in-jquery/" rel="bookmark" title="$.ajax in jQuery">$.ajax in jQuery </a></li>
<li><a href="/2009/04/01/jquery-accessing-a-child-element/" rel="bookmark" title="jQuery &#8211; accessing a child element">jQuery &#8211; accessing a child element </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/24/2xjquery-select-a-selector/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
