1class ClassMates{
2 constructor(name,age){
3 this.name=name;
4 this.age=age;
5 }
6 displayInfo(){
7 return this.name + "is " + this.age + " years old!";
8 }
9}
10
11let classmate = new ClassMates("Mike Will",15);
12classmate.displayInfo(); // result: Mike Will is 15 years old!
1class Rectangle {
2 constructor(height, width) {
3 this.height = height;
4 this.width = width;
5 }
6 // Getter
7 get area() {
8 return this.calcArea();
9 }
10 // Method
11 calcArea() {
12 return this.height * this.width;
13 }
14}
15
16const square = new Rectangle(10, 10);
17
18console.log(square.area); // 100
1//use classes by initiating one like so:
2class MyClass {
3 constructor(FirstProperty, SecondProperty, etcetera) {
4 //The constructor function is called with the new class
5 //instance's parameters, so this will be called like so:
6 //var classExample = new MyClass("FirstProperty's Value", ...)
7 this.firstProperty = FirstProperty;
8 this.secondProperty = SecondProperty;
9 }
10 //creat methods just like functions:
11 method(Parameters) {
12 //Code Here
13 }
14 //getters are properties that are calculated when called, versus fixed
15 //variables, but still have no parenthesis when used
16 get getBothValues()
17 {
18 return [firstProperty, secondProperty];
19 }
20}
21//Note: this is all syntax sugar reducing the boilerplate versus a
22// function-defined object.
1//private but is a good example
2class includes {
3 constructor(){}
4
5 inculde_apps(file) {
6 var script = document.createElement('script');
7 script.src = "ext/apps/"+file;
8 script.type = 'text/javascript';
9 document.getElementsByTagName('body').item(0).appendChild(script);
10 }
11
12 inculde_scripts(file) {
13 var script = document.createElement('script');
14 script.src = "ext/scripts/"+file;
15 script.type = 'text/javascript';
16 document.getElementsByTagName('body').item(0).appendChild(script);
17 }
18
19 inculde_css(file) {
20 var script = document.createElement('link');
21 script.rel = "stylesheet";
22 script.type = 'text/css';
23 script.href = "css/"+file;
24 document.getElementsByTagName('head').item(0).appendChild(script);
25 }
26 }
1class SayHelloTo {
2 name (to) {
3 console.log(`Hello ${to}`);
4 }
5 constructor (to) {
6 this.name(to);
7 }
8}
9const helloWorld = new SayHelloTo(World);
1let Person = class {
2 constructor(firstName, lastName) {
3 this.firstName = firstName;
4 this.lastName = lastName;
5 }
6}