title: Create a model (POJO) in JavaScript
body: <p>can you help me with a doubt?</p>
  
  <p>I'm trying an approach more didatical in JS, creatind a POJO/POCO for a model class.</p>
  
  <p>I have a Person object:</p>
  
  <pre><code>function person(name, age, gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        return this;
  }
  </code></pre>
  
  <p>Now I can create some data:</p>
  
  <pre><code>var personalities = new Array(new person("Alicia", 152, "F"), 
                                new person("Bartolomeu Simpson", 36, "M"),
                                new person("Ayrton Senna", 58, "M"),
                                new person("Jean J. Michel", 36, "M"),
                                new person("Jean J. Michel", 37, "M"));
  </code></pre>
  
  <p>Well, done it if I call sort method there is no action to order this data:</p>
  
  <pre><code>personalities.sort();
  
  for(var i = 0; i &lt; personalities.length; i++)
    console.log(personalities[i].name);
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>Alicia
  Bartolomeu Simpson
  Ayrton Senna
  Jean J. Michel
  Jean J. Michel
  </code></pre>
  
  <p>I created a toString() method:</p>
  
  <pre><code>function person(name, age, gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  
    this.toString = function() {
      return this.name + " " + 
             this.age + " years old" + 
             (this.gender == 'M' ? " man" : " woman");
    };
  
    return this;
  }
  </code></pre>
  
  <p>Now the sort method is "ok", the output now is:</p>
  
  <pre><code>Alicia
  Ayrton Senna
  Bartolomeu Simpson
  Jean J. Michel
  Jean J. Michel
  </code></pre>
  
  <p>But I want do something different, order by name and age (oldest first). I implemented the method compareTo(obj):</p>
  
  <pre><code>function person(name, age, gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  
    this.toString = function() {
      return this.name + " " + 
             this.age + " years old" + 
             (this.gender == 'M' ? " man" : " woman");
    };
  
    this.compareTo = function(otherPerson) {
      var resultOfComparison = 0;
  
      if(this.name &gt; otherPerson.name) {
        resultOfComparison = 1;
        return resultOfComparison;
      }
      else if(this.name &lt; otherPerson.name) {
        resultOfComparison = -1;
        return resultOfComparison;
      }
      else if(this.age &gt; otherPerson.age) {
        resultOfComparison = -1;
        return resultOfComparison;
      }
      else if(this.age &lt; otherPerson.age) {
        resultOfComparison = 1;
        return resultOfComparison;
      }
  
      return resultOfComparison;
    };
  
    return this;
  }
  </code></pre>
  
  <p>The method is ok, look it:</p>
  
  <pre><code>var jean1 = new person("Jean J. Michel", 36, "M");
  var jean2 = new person("Jean J. Michel", 37, "M");
  
  console.log(jean1.compareTo(jean2)); //1
  console.log(jean1.compareTo(jean1)); //0
  console.log(jean2.compareTo(jean1)); //-1
  </code></pre>
  
  <p>Can I use this approach to sort my array? Something like this:</p>
  
  <pre><code>personalities.sort(person.compareTo);
  </code></pre>
  
  <p>I really do not want to do it in a service class that use my model:</p>
  
  <pre><code>personalities.sort(function(personA, personB) {
                       var resultOfComparison = 0;
  
                       if(personA.name &gt; personB.name) {
                         resultOfComparison = 1;
                         return resultOfComparison;
                       }
                       else if(personA.name &lt; personB.name) {
                         resultOfComparison = -1;
                         return resultOfComparison;
                       }
                       else if(personA.age &gt; personB.age) {
                         resultOfComparison = -1;
                         return resultOfComparison;
                       }
                       else if(personA.age &lt; personB.age) {
                         resultOfComparison = 1;
                         return resultOfComparison;
                       }
  
                       return resultOfComparison;
                     });
  </code></pre>
  
  <p>I think that this is a model responsibility.</p>
  
  <p>Thanks for all help.</p>