<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
	>
<channel>
	<title>Comments on: Object-Oriented Javascript and SproutCore</title>
	<atom:link href="http://davidvanvickle.com/blog/2008/06/27/podcast-episode-9-object-oriented-javascript-and-sproutcore/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidvanvickle.com/blog/2008/06/27/podcast-episode-9-object-oriented-javascript-and-sproutcore/</link>
	<description>Sedentary web designer explores fitness.</description>
	<lastBuildDate>Fri, 19 Mar 2010 18:32:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Danny Graham</title>
		<link>http://davidvanvickle.com/blog/2008/06/27/podcast-episode-9-object-oriented-javascript-and-sproutcore/comment-page-1/#comment-928</link>
		<dc:creator>Danny Graham</dc:creator>
		<pubDate>Wed, 23 Dec 2009 07:00:20 +0000</pubDate>
		<guid isPermaLink="false">http://vanturtle.wordpress.com/?p=107#comment-928</guid>
		<description>This is similar in some ways to SJL - however, SproutCore has really gone above and beyond in terms of creating their own object model.  I would also look at Google&#039;s Closure - which is much more similar to SJL.  http://code.google.com/closure/

Just a comment about JavaScript object types.  I think it&#039;s a bit misleading to classify these types as Singleton/Custom.  Rather I would say - would you like an object with a static vs instantiable?  MyObject = function(){}; roughly amounts to the same thing as MyObject = {};  - This difference is when you can create an instance of the function object.  Let&#039;s look at the difference:

MyClass = function(){
  this.Name = &quot;Dog&quot;;
  this.Sound = &quot;Bark&quot;;
  
  var Owner = &quot;Peter Gibbons&quot;

  var ChangeOwner = function(strNewOwner) { Owner = strNewOwner };

  this.MakeSound = function() { return this.Sound; };
  this.CheckPapers = function(strOwnerName) {
    if (NO_ONE_IS_LOOKING &amp;&amp; strOwnerName !== Owner) ChangeOwner(strOwnerName);
    return Owner; 
  };
};

MyClass.prototype.PutDown = function() { return &quot;Killed &quot; + this.Name; };
MyClass.Kill = function(oAnimal) { oAnimal.PutDown(); };

vs

MyStaticClass = {
  Name: &quot;Dog&quot;,
  Sound: &quot;Bark&quot;,
  Owner: &quot;Frank Wallace&quot;,
  ChangeOwner: function(strNewOwner) { this.Owner = strNewOwner },
  MakeSound: function() { return this.Sound; },
  .......
};

In OOP terms we have basically:

public class MyObject {} vs public static class MyObject{}

One requires a constructor (the function object) and one cannot be instantiated at all but can only be accessed statically.  With a function object when you call var myObj = new MyClass(); you now get an instance of the object MyClass and the MyClass function is executed as the constructor.

Instantiable objects, like in most OOP worlds, do not prevent you from accessing or working with them in a static way.  In our example MyClass has the static method Kill.  You do not need to create an instance of MyClass to call the Kill method.  But you will also notice that MyClass has a prototyped method defined.  This means that this method will be a part of any new instance you create.  Call it a class method.

Also notice the use of &quot;var&quot; within MyClass.  The use of a constructor allows us to create private variables and methods that are a part of the instance.  In addition we can expose public methods through the constructor that have access to this local variable scope - as demonstrated above.

Here&#039;s the &quot;Singleton&quot; distinction.  The Singleton pattern does not directly apply to static classes or static objects.  The Singleton pattern is necessary when you have a class that you DO need to create one (or perhaps more) instance of but you want to ensure that multiple unnecessary instance are not created.  Exemplified below.

Method A) Define the Object and instantiate it one time right away.
Page = new (function() { 
  // Constructor logic
  // private vars
  var a = &quot;&quot;;
  // private methods
  var b = function() {};
  // public methods
  this.c = function() {};
})();

And that&#039;s it.  Since Page is already an instance there is no need to go further.  In this method it is NOT possible to create a new instance of Page, nor is it possible to access Page statically since it is now an active instance.

Method B) Define a static Object and use the Singleton method as a constructor of sorts

Page = {
  instance: null,
  _constructor: function() {
    // private vars
    // private methods
    // public methods
   return this;
  },
  GetInstance: function() { if (!Page.instance) Page.instance = Page._constructor(); return Page.instance; }
};

That&#039;s kinda crappy cause you can bound up in a bunch of stuff

Method C)  Define a function object and use the Singleton pattern

Page = function() {
  // private vars
  // private methods
  // public methods
}

// Group prototype definitions
Page.prototype = {
  methodName: function(){},
  methodName2: function(){}
};

// Public method
Page.prototype.methodName = function() {};

// Static property
Page.instance = null;

// Static methods
Page.GetInstance = function() { if (!Page.instance) Page.instance = new Page(); return Page.instance; };

I like this the best - it&#039;s just the cleanest.  With this method I can make a new Page instance if I really want to by saying myExtraPage = new Page(); - But if I plan to access this class according to the singleton pattern I&#039;ll say var myPage = Page.GetInstance();  - If the class has not been instantiated yet, then it will make sure to do that for me.

This is great - it gives me public and private methods and properties as well as public static methods and properties.  I also find this method better lends itself to extensibility and reuse.

So to sum up - I would reserve the term Singleton when dealing with class objects (function objects) that you want only one instance of.</description>
		<content:encoded><![CDATA[<p>This is similar in some ways to SJL &#8211; however, SproutCore has really gone above and beyond in terms of creating their own object model.  I would also look at Google&#8217;s Closure &#8211; which is much more similar to SJL.  <a href="http://code.google.com/closure/" rel="nofollow">http://code.google.com/closure/</a></p>
<p>Just a comment about JavaScript object types.  I think it&#8217;s a bit misleading to classify these types as Singleton/Custom.  Rather I would say &#8211; would you like an object with a static vs instantiable?  MyObject = function(){}; roughly amounts to the same thing as MyObject = {};  &#8211; This difference is when you can create an instance of the function object.  Let&#8217;s look at the difference:</p>
<p>MyClass = function(){<br />
  this.Name = &#8220;Dog&#8221;;<br />
  this.Sound = &#8220;Bark&#8221;;</p>
<p>  var Owner = &#8220;Peter Gibbons&#8221;</p>
<p>  var ChangeOwner = function(strNewOwner) { Owner = strNewOwner };</p>
<p>  this.MakeSound = function() { return this.Sound; };<br />
  this.CheckPapers = function(strOwnerName) {<br />
    if (NO_ONE_IS_LOOKING &amp;&amp; strOwnerName !== Owner) ChangeOwner(strOwnerName);<br />
    return Owner;<br />
  };<br />
};</p>
<p>MyClass.prototype.PutDown = function() { return &#8220;Killed &#8221; + this.Name; };<br />
MyClass.Kill = function(oAnimal) { oAnimal.PutDown(); };</p>
<p>vs</p>
<p>MyStaticClass = {<br />
  Name: &#8220;Dog&#8221;,<br />
  Sound: &#8220;Bark&#8221;,<br />
  Owner: &#8220;Frank Wallace&#8221;,<br />
  ChangeOwner: function(strNewOwner) { this.Owner = strNewOwner },<br />
  MakeSound: function() { return this.Sound; },<br />
  &#8230;&#8230;.<br />
};</p>
<p>In OOP terms we have basically:</p>
<p>public class MyObject {} vs public static class MyObject{}</p>
<p>One requires a constructor (the function object) and one cannot be instantiated at all but can only be accessed statically.  With a function object when you call var myObj = new MyClass(); you now get an instance of the object MyClass and the MyClass function is executed as the constructor.</p>
<p>Instantiable objects, like in most OOP worlds, do not prevent you from accessing or working with them in a static way.  In our example MyClass has the static method Kill.  You do not need to create an instance of MyClass to call the Kill method.  But you will also notice that MyClass has a prototyped method defined.  This means that this method will be a part of any new instance you create.  Call it a class method.</p>
<p>Also notice the use of &#8220;var&#8221; within MyClass.  The use of a constructor allows us to create private variables and methods that are a part of the instance.  In addition we can expose public methods through the constructor that have access to this local variable scope &#8211; as demonstrated above.</p>
<p>Here&#8217;s the &#8220;Singleton&#8221; distinction.  The Singleton pattern does not directly apply to static classes or static objects.  The Singleton pattern is necessary when you have a class that you DO need to create one (or perhaps more) instance of but you want to ensure that multiple unnecessary instance are not created.  Exemplified below.</p>
<p>Method A) Define the Object and instantiate it one time right away.<br />
Page = new (function() {<br />
  // Constructor logic<br />
  // private vars<br />
  var a = &#8220;&#8221;;<br />
  // private methods<br />
  var b = function() {};<br />
  // public methods<br />
  this.c = function() {};<br />
})();</p>
<p>And that&#8217;s it.  Since Page is already an instance there is no need to go further.  In this method it is NOT possible to create a new instance of Page, nor is it possible to access Page statically since it is now an active instance.</p>
<p>Method B) Define a static Object and use the Singleton method as a constructor of sorts</p>
<p>Page = {<br />
  instance: null,<br />
  _constructor: function() {<br />
    // private vars<br />
    // private methods<br />
    // public methods<br />
   return this;<br />
  },<br />
  GetInstance: function() { if (!Page.instance) Page.instance = Page._constructor(); return Page.instance; }<br />
};</p>
<p>That&#8217;s kinda crappy cause you can bound up in a bunch of stuff</p>
<p>Method C)  Define a function object and use the Singleton pattern</p>
<p>Page = function() {<br />
  // private vars<br />
  // private methods<br />
  // public methods<br />
}</p>
<p>// Group prototype definitions<br />
Page.prototype = {<br />
  methodName: function(){},<br />
  methodName2: function(){}<br />
};</p>
<p>// Public method<br />
Page.prototype.methodName = function() {};</p>
<p>// Static property<br />
Page.instance = null;</p>
<p>// Static methods<br />
Page.GetInstance = function() { if (!Page.instance) Page.instance = new Page(); return Page.instance; };</p>
<p>I like this the best &#8211; it&#8217;s just the cleanest.  With this method I can make a new Page instance if I really want to by saying myExtraPage = new Page(); &#8211; But if I plan to access this class according to the singleton pattern I&#8217;ll say var myPage = Page.GetInstance();  &#8211; If the class has not been instantiated yet, then it will make sure to do that for me.</p>
<p>This is great &#8211; it gives me public and private methods and properties as well as public static methods and properties.  I also find this method better lends itself to extensibility and reuse.</p>
<p>So to sum up &#8211; I would reserve the term Singleton when dealing with class objects (function objects) that you want only one instance of.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
