Classes


ES 2009 Syntax

function myObject(property1, property2) { 
   this.property1 = property1;
   this.property2 = property2;
}

var myObject = { 
   property1 : MyProp,
   method1_dosomething : function() {
      console.log('something');
   },
   property_Literal : "MyName",
   property2_Array : [ {arrayelement1 : "one"} {arrayelement2 : "two"} ];
}

ES 2015 Syntax

Classes are just special types of functions with additional functionality.
There are two ways you can define a class: declaration or an expression.
You always need to declare your class before you use it.


Definition - Declaration

Class declarations are not hoisted (like function declarations)

class myObject { 
   constructor(property1, property2) {
      this.property1 = property1;
      this.property2 = property2;
   }

   method1_dosomething() {
      console.log('something');
   }
}


Definition - Class Expression

These can be named or unnamed


let Something = class Something { 
   constructor(myheight, mywidth) {
      this.height = myheight;
      this.width = mywidth;
   }
};
console.log(Something.name);


Extending / Inheriting

class myNewObject extends myObject { 
   constructor(property1) {
      super(property1)
   }
}


List Of Objects

var list = [ 
   { name: 'David', id: 42 },
   { name: 'Steven', id: 65 }
];


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopNext