News for hackers. Stuff that matters.
Re:Preparing Yourself for Modern JavaScript Development by aymeric on Friday July 06, 2012 @05:26PM (#4209733)
What is the difference between using the prototype technique shown in the article and this?

var Person = function (){ this.Save = function() { … }; };

var person = new Person(); person.Save();

Re:Preparing Yourself for Modern JavaScript Development by jamiemill on Friday July 06, 2012 @07:14PM (#4210033)
That's called a "privileged method". The advantage of those is that they can access any private variables from the constructor, and they keep those variables in scope for their lifetime.

The disadvantage however, is that the method is re-created once for each instance when the constructor runs, so they are not as memory-efficient as the prototype style.

Also, when you add methods to the prototype you affect all instances from the past and the future. So for example if you add a method to String.prototype, all current and future strings get it.

Re:Preparing Yourself for Modern JavaScript Development by aymeric on Friday July 06, 2012 @09:42PM (#4210412)
Should I be concerned about the additional memory my way of doing things takes? It seems more convenient, allows for private methods and a limited drawback in my view (memory is rarely a bottleneck)
Re:Preparing Yourself for Modern JavaScript Development by jamiemill on Saturday July 07, 2012 @05:55AM (#4211248)
Probably depends on your application. You may also see slower construction time if you are creating thousands of objects.

I think which to use depends on whether you believe private methods/variables are helpful or not. Some argue they are unnecessary and it's better to keep everything public. Some like to use a naming convention like an underscore prefix on private items, but declare them publicly.

You could also argue that private items are problematic if you start composing classes from other classes by extending. Then the new methods can't access the private state because they're in a different scope, whereas if they were on the prototype they could find it through `this`.

I've used both approaches but nowadays tend towards the prototype style as that seems to be the more accepted.

Re:Preparing Yourself for Modern JavaScript Development by esailija on Saturday July 07, 2012 @08:41AM (#4211482)
There are also other problems such as nesting functions inside other functions inside other functions which is IMO much harder to maintain than a clean flat list of functions defined in the prototype. Don't even get me started on using "self", "that" or "me" instead of the built-in keyword "this".

It could be just me but I cannot understand how that was/is/has been acceptable at all in the first place. Doesn't functions nested 11 levels deep (true story) ring any alarm bells for people?

Re:Preparing Yourself for Modern JavaScript Development by drostie on Saturday July 07, 2012 @08:58AM (#4211517)
Memory is seldom a constraint on the sorts of computers which run JS. The real WTF about that approach later on is just going to be that person.hasOwnProperty('Save') === true. As long as you can guarantee that you will never want to iterate over a person with a for/in loop, you should be pretty much fine.
Re:Preparing Yourself for Modern JavaScript Development by ricardobeat on Saturday July 07, 2012 @12:01AM (#4210686)
why private methods?
Re:Preparing Yourself for Modern JavaScript Development by aymeric on Saturday July 07, 2012 @01:52AM (#4210855)
var Person = function() { var privateMethod = function() {...}; };
Re:Preparing Yourself for Modern JavaScript Development by swah on Saturday July 07, 2012 @08:57AM (#4211514)
Normally I use code like this to avoid polluting the global namespace -- you might as well throw your functions in there and they will be "private":

    (function(){ 

     })();
Re:Preparing Yourself for Modern JavaScript Development by ricardobeat on Saturday July 07, 2012 @04:40PM (#4212651)
why: what's your reasoning for using them?

A more memory-efficient approach:

    var Person = (function(){
  
      function Person(x){
        this.x = x
      }
      
      function myPrivateMethod(x){
        //...
      }

      Person.prototype.something = function(){
        y = myPrivateMethod(this.x)
      }
    
      return Person 
    })()