Advertisement
Noah-Huppert

JS Models are good

Dec 13th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //The real difference is when you add functions into the mix, lets say you want to perform an operation on the data.
  2. //This could be a simple as turning it into a format or saving it to the server. The thing is, it is a lot easier when
  3. //You use classes as data Models.
  4. function Foo(a, b){
  5.     var self = this;
  6.    
  7.     self.a = a;
  8.     self.b = b;
  9.  
  10.     self.turnIntoFormat = function(){
  11.         return a + ": " + b;
  12.     };
  13. }
  14.  
  15. var data = [new Foo("a", "b"), new Foo("aa", "bb")];
  16.  
  17. //VS this, which is easier?
  18. var data = [
  19.     {
  20.         "a": "a",
  21.         "b": "bb",
  22.                 turnInfoFormat: function(){
  23.             return a + ": " + b;
  24.         };
  25.     },
  26.     {
  27.         "a": "aa",
  28.         "b": "bb",
  29.                 turnInfoFormat: function(){
  30.             return a + ": " + b;
  31.         };
  32.     }
  33. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement