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);

Constructor

This is a special method used for creating and initializing the object.
A constructor can use the "super" keyword to call the constructor of the super class.
You always need to call "super" when defining the constructor of a subclass
All React component classes that have a constructor should start it with a super(props) call.



Extending / Inheriting

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


List Of Objects

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

A empty object literal

var myObject = { } 




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