<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>DOM Scripting comments: A question of style</title>
        <link>http://domscripting.com/blog/display/89</link>
        <description>I want your opinion on coding style, please.</description>
        <language>en</language>
        <item>
            <title>Jeff Cutsinger</title>
            <link>http://domscripting.com/blog/display.php/89#comment698</link>
            <content:encoded><![CDATA[<p>Sorry, didn&#8217;t realize links weren&#8217;t allowed. The url for a reference is <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object:watch">http://developer.mozilla.org/en/docs/Core<em>JavaScript</em>1.5<em>Reference:Global</em>Objects:Object:watch</a></p>
]]></content:encoded>
            <pubDate>Thu, 09 Nov 2006 21:58:17 GMT</pubDate>
        </item>
        <item>
            <title>Jeff Cutsinger</title>
            <link>http://domscripting.com/blog/display.php/89#comment697</link>
            <content:encoded><![CDATA[<p>Do you know how well supported watch and unwatch are? if they are well supported, it would make the first choice much, much better.</p>
]]></content:encoded>
            <pubDate>Thu, 09 Nov 2006 21:56:41 GMT</pubDate>
        </item>
        <item>
            <title>Dustin Diaz</title>
            <link>http://domscripting.com/blog/display.php/89#comment696</link>
            <content:encoded><![CDATA[<p>To this day I still have no idea how to post code on this website and make it look good. Will you be a good lad and share with us your comment input policy?</p>
]]></content:encoded>
            <pubDate>Wed, 08 Nov 2006 01:25:38 GMT</pubDate>
        </item>
        <item>
            <title>Dustin Diaz</title>
            <link>http://domscripting.com/blog/display.php/89#comment695</link>
            <content:encoded><![CDATA[<p>You could effectively pass in your values to the constructor - by passing the extra setters for the client code.</p>

<p>var F = function(a, b) {
    this.init(a, b);
};
Foo.prototype = {
    setA : function(name) {</p>

<pre><code>},
setB : function(name) {

},
init : function(a, b) {
    this.setA(a);
    this.setB(b);
}
</code></pre>

<p>};</p>

<p>/*
  * instantiate :)
*/</p>

<p>var o = new F(&#8216;hello&#8217;, &#8216;world&#8217;);</p>

<p>But yeah, if I had to pick - I&#8217;d pick your second option (over the first) Jeremy :)</p>
]]></content:encoded>
            <pubDate>Wed, 08 Nov 2006 01:23:52 GMT</pubDate>
        </item>
        <item>
            <title>jens persson</title>
            <link>http://domscripting.com/blog/display.php/89#comment694</link>
            <content:encoded><![CDATA[<p>For the show of hands, I put both of my hands up for number one. I simply disgusted by getters and setters as separate (public) functions. If you need to protect the interface to  members via functions, one should be able to to declare accessors like you do in python [1] or delphi [2].</p>

<p>I know that this isn&#8217;t supported in javascript, but as an answer to what I like best it&#8217;s the answer.</p>

<p>[1] <a href="http://docs.python.org/lib/built-in-funcs.html#l2h-57">http://docs.python.org/lib/built-in-funcs.html#l2h-57</a></p>

<p>[2] <a href="http://www.delphibasics.co.uk/RTL.asp?Name=Property">http://www.delphibasics.co.uk/RTL.asp?Name=Property</a></p>
]]></content:encoded>
            <pubDate>Tue, 07 Nov 2006 18:14:37 GMT</pubDate>
        </item>
        <item>
            <title>Jeremy Keith</title>
            <link>http://domscripting.com/blog/display.php/89#comment693</link>
            <content:encoded><![CDATA[<p>I&#8217;ve kind of got private methods in both examples: the init method is public, the execute method is private. See how init is set using the this keyword, but execute is set using var? Using var inside an object like that effectively creates something that&#8217;s private (although it can always be overwritten through the prototype).</p>

<p>Douglas Crockford has more to say on this:
<a href="http://javascript.crockford.com/private.html">http://javascript.crockford.com/private.html</a></p>
]]></content:encoded>
            <pubDate>Thu, 02 Nov 2006 12:24:40 GMT</pubDate>
        </item>
        <item>
            <title>André Luís</title>
            <link>http://domscripting.com/blog/display.php/89#comment692</link>
            <content:encoded><![CDATA[<p>For the quick show of hands, I prefer the first.</p>

<p>If you&#8217;re not going to make any verifications, go straight ahead and set the vars straight away.. but sometimes (&quot;it depends&quot;) you might want that little layer for verification purposes, so you can have hybrids. Some properties have getter/setters others don&#8217;t&#8230;</p>

<p>This would make much more sense if you could protect variables against public access, ie, specify the level of access you wanted for each property (public, private, protected&#8230; as in Java or other OO languages). Is there a way to do this I don&#8217;t know of? If there isn&#8217;t, then it actually makes no difference, since someone using that object can get/set the value without using the getters/setters.</p>
]]></content:encoded>
            <pubDate>Thu, 02 Nov 2006 04:08:32 GMT</pubDate>
        </item>
        <item>
            <title>Jeremy Keith</title>
            <link>http://domscripting.com/blog/display.php/89#comment691</link>
            <content:encoded><![CDATA[<p>So, I may be paraphrasing everyone&#8217;s responses here, but it seems like version 2 is the more &quot;correct&quot; programmatically because it allows you to check the values before setting them, but version 1 is generally more obvious and readable, at least to people who aren&#8217;t necessarily coming from a higher level programming language.</p>

<p>It seems that there&#8217;s general agreement on the answer: it depends. It depends on what you&#8217;re doing and the complexity of your script.</p>

<p>Thank you everybody for chiming in. I appreciate it.</p>
]]></content:encoded>
            <pubDate>Sun, 29 Oct 2006 17:02:06 GMT</pubDate>
        </item>
        <item>
            <title>Lachlan Hunt</title>
            <link>http://domscripting.com/blog/display.php/89#comment690</link>
            <content:encoded><![CDATA[<p>Generally, I prefer the second because it allows you to do check the value before you set it, so it prevents it from being set to an illegal value.  Although, in your short examples, any value is valid effectively valid, so the first would be fine.</p>

<p>But say, for example, your object depended upon foo and bar being integers within a certain range, youâ€™d have to do the second so that you could check.</p>

<p>The other alternative, which is the best of both worlds, is to do the following.  Unfortunately, browser support is an issue here.</p>

<p><pre><code>function Foobar() {
    var _bar = 0;
    this.__defineGetter__("bar", function() { return _bar; });
    this.__defineSetter__("bar", function(value) {
        if (value >= 0 && value <= 100) _bar = value; 
    });
}

var x = new Foo();
x.bar = 10;
x.bar = 150;
alert(x.bar);</code></pre></p>
]]></content:encoded>
            <pubDate>Sun, 29 Oct 2006 15:01:58 GMT</pubDate>
        </item>
        <item>
            <title>Quadro</title>
            <link>http://domscripting.com/blog/display.php/89#comment689</link>
            <content:encoded><![CDATA[<p>Reading your post made me think you wrote a plugin for Foobar2000</p>
]]></content:encoded>
            <pubDate>Sun, 29 Oct 2006 11:27:39 GMT</pubDate>
        </item>
        <item>
            <title>Al</title>
            <link>http://domscripting.com/blog/display.php/89#comment688</link>
            <content:encoded><![CDATA[<p>as someone who first learned Java, then struggled to learn and read Javascript.. i prefer the latter.. but still find the fact that you need to call the overall object/class a &quot;function&quot;, then you have more &quot;functions&quot; inside of that, confusing.</p>

<p>Java describes it cleanly.. i.e. a a Class encapsulates attributes and methods..  and the word &quot;Class&quot; isn&#8217;t re-used again for describing methods.   A Class is a class and a method is a method.   Of course, there are inner Classes as well.. but they act like a Class.  There is no ambiguity.</p>

<p>Flexibility is handy of course.  I love Perl as well, where there are many way to do the same thing.. but calling all these things a &quot;function&quot; in javascript is confusing and makes it challenging to debug things for those of us who learned an objected oriented language before javascript.</p>

<p>P.S. love the Dom Scripting book.. Great piece of work.</p>
]]></content:encoded>
            <pubDate>Fri, 27 Oct 2006 20:56:59 GMT</pubDate>
        </item>
        <item>
            <title>Jeff Cutsinger</title>
            <link>http://domscripting.com/blog/display.php/89#comment687</link>
            <content:encoded><![CDATA[<p>Definitely the latter. Encapsulation is good in any language in any setting, period. If you need to change the behavior of those variables (and you will), it&#8217;ll be a lot easier with the second method. Now if only JS had properties a la C#, you could have the best of both worlds.</p>
]]></content:encoded>
            <pubDate>Thu, 26 Oct 2006 16:12:45 GMT</pubDate>
        </item>
        <item>
            <title>Martin</title>
            <link>http://domscripting.com/blog/display.php/89#comment686</link>
            <content:encoded><![CDATA[<p>JavaScript is JavaScript. It doesn&#8217;t need to be conforming of other programming languages&#8217; methods so I&#8217;d go with the first one because that&#8217;s the real benefit of JavaScript: simple, easy to read, and it rocks!</p>
]]></content:encoded>
            <pubDate>Thu, 26 Oct 2006 14:02:27 GMT</pubDate>
        </item>
        <item>
            <title>Matthias Miller</title>
            <link>http://domscripting.com/blog/display.php/89#comment685</link>
            <content:encoded><![CDATA[<p>Given these two options, i would certain prefer the latter. In the first, there&#8217;s nothing in the interface that even mentions foo or bar, so it&#8217;s unnatural to expect the caller to know to set them. Also, as Oli mentioned right off, the abstraction provides valuable protection.</p>

<p>To keep it simple, I&#8217;d propose a third option:</p>

<p>baz.init({foo: 1, bar: 2});</p>
]]></content:encoded>
            <pubDate>Thu, 26 Oct 2006 04:46:39 GMT</pubDate>
        </item>
        <item>
            <title>Jeff Watkins</title>
            <link>http://domscripting.com/blog/display.php/89#comment684</link>
            <content:encoded><![CDATA[<p>Jeremy, the second example will create a new instance of each method every time you create a new object. So if you have:</p>

<pre><code>var baz1= new Foobar();
var baz2= new Foobar();

baz1.setFoo != baz2.setFoo
</code></pre>

<p>This can be a <em>big</em> problem for memory usage.</p>

<p>You&#8217;re almost always better off using the prototype method (for all its warts). On the other hand, I often use nested functions to achieve much the same effect as private methods.</p>

<p>And don&#8217;t get me started about the ugly prototype (the library) practice of declaring classes.</p>
]]></content:encoded>
            <pubDate>Thu, 26 Oct 2006 02:00:20 GMT</pubDate>
        </item>
        <item>
            <title>Tim Huegdon</title>
            <link>http://domscripting.com/blog/display.php/89#comment683</link>
            <content:encoded><![CDATA[<p>Everybody knows that the best programmers are lazy and dumb (<a href="http://blog.outer-court.com/archive/2005-08-24-n14.html">http://blog.outer-court.com/archive/2005-08-24-n14.html</a>), so I&#8217;d probably opt for option #2 because it takes less effort. However, as Oli points out in the first comment, using option #1 (accessor methods) provides you with an extra layer of abstraction that allows you to either filter the data being assigned to the object member, or add extra logic - it&#8217;s also an accepted practice.</p>

<p>With all this in mind, my answer would be: whichever method fits the bill! ;-)</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 22:57:26 GMT</pubDate>
        </item>
        <item>
            <title>Mufasa</title>
            <link>http://domscripting.com/blog/display.php/89#comment682</link>
            <content:encoded><![CDATA[<p>The first one. JavaScript is supposed to be simple, so if you really want more formal class definitions, switch to that language. KISS when it comes to JavaScript.</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 22:49:17 GMT</pubDate>
        </item>
        <item>
            <title>John Riviello</title>
            <link>http://domscripting.com/blog/display.php/89#comment681</link>
            <content:encoded><![CDATA[<p>I prefer the first method because it is easier for me to read and understand, most likely because it is similar to how I first learned to write objects in JS. As you pointed out, the second one looks more like server-side code, and since I&#8217;m a front-end developer, I have no desire to write my JS code in that format. Shorter JS code works best for me.</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 22:15:06 GMT</pubDate>
        </item>
        <item>
            <title>Steve Tucker</title>
            <link>http://domscripting.com/blog/display.php/89#comment680</link>
            <content:encoded><![CDATA[<p>I use the second method quite heavily in PHP, but not so much in Javascript. I think the decision on which technique to use comes down to the importance of the variable. Were I to anticipate that the variable would be often reasigned from an outside source I would use method #2, otherwise I would stick with method #1. Additionally the number of variables would play a part. Should there be a large quantity of variables to set I would use method #1, otherwise method #2.</p>

<p>I&#8217;ve always found I use more shortcuts and a compact writing style with Javascript as opposed to other languages. For readability purposes I would write method #2 as</p>

<p>function Foobar() {</p>

<pre><code>var foo,bar;
this.setFoo = function(value) { foo = value; };
this.setBar = function(value) { bar = value; };
this.init = function() {
    execute();
};
var execute = function() {
    alert(foo bar);
};
</code></pre>

<p>}</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 21:03:54 GMT</pubDate>
        </item>
        <item>
            <title>Philip</title>
            <link>http://domscripting.com/blog/display.php/89#comment679</link>
            <content:encoded><![CDATA[<p>Coming from a Cplusplus  /Java/Perl background, I&#8217;d say follow this rule of thumb:</p>

<p>If you have restrictions on values that your member variables can have, then use a setter/getter.  If you don&#8217;t, avoid them.  Also, when using setters/getters, I find that it&#8217;s better to have a short meaningful name for your setter/getter than to reserve that for your private variable and give the setter/getter a more verbose name.</p>

<p>Therefore, I&#8217;d do something like:</p>

<p>var _foo, _bar;</p>

<p>this.foo = function(value)
{
   if(arguments.length == 1)
      _foo = value;
   return _foo;
}</p>

<p>That way, you can call it like:</p>

<p>obj.foo(3);</p>

<p>alert(obj.foo());</p>

<p>PS: Excuse the formatting, I couldn&#8217;t figure out how to get pre working in the comments
PPS: I also couldn&#8217;t figure out how to enter a plus sign into the comments - the preview seems to strip them off.</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 20:30:05 GMT</pubDate>
        </item>
        <item>
            <title>Mark Kahn</title>
            <link>http://domscripting.com/blog/display.php/89#comment678</link>
            <content:encoded><![CDATA[<p>Neither.  I prefer to use prototypes because every time I don&#8217;t I find myself (or rather collegues editing my code) blowing the lid off of IE6&#8217;s memory management.  An hour after working with a complicated js site and IE is using 1gb of RAM.</p>

<p>A while back I wrote a function to implement class-based inheritance (similiar to Dean Edwards&#8217; Base Class) and told everyone to use that and reference EVERYTHING using &quot;this.&quot;  It doesn&#8217;t exactly prevent memory leaks, but it certainly helps to prevent them, and I have one less path to check when I&#8217;m trying to find them.</p>

<p>for obvious reasons, foobar.prototype.setbar is going to (potentially) take up much less memory in the long run than foobar.setbar.  It would be nice to find a way to implement private variables using prototypes but unfortunately one doesn&#8217;t exist (yet).</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 17:32:42 GMT</pubDate>
        </item>
        <item>
            <title>Oli</title>
            <link>http://domscripting.com/blog/display.php/89#comment677</link>
            <content:encoded><![CDATA[<p>I think it&#8217;s good to have a layer of abstraction, so you can build logic into your property get / set if necessary. If a consumer is directly accessing the member variables, you have no level of control about what gets returned. In a more full-blown programming language these members would typically be private, allowing you to encaspulate your implementation and keep it seperate from the public interface to your class. </p>

<p>Taking this approach allows you to expose properties that might not correspond to directly returning the value of a single member variable - for example, you could expose a &#8216;FullName&#8217; property that returns a value based on title, first name, and surname.</p>

<p>So I&#8217;d go for option 2, but I can understand wanting to keep it simple&#8230;</p>
]]></content:encoded>
            <pubDate>Wed, 25 Oct 2006 16:49:59 GMT</pubDate>
        </item>
   </channel>
</rss>