javascript classes with class or function keyword code example

Example 1: javascript new function as class

// Javascript Function that behaves as class, with private variables
function Person(name){
    const birth = new Date();
    this.greet = () => `Hello my name is ${name}. I was born at ${birth.toISOString()}`;
}

const joe = new Person("Joe");
joe.greet(); // Hello my name is Joe. I was born at 2021-04-09T21:12:33.098Z
// The birth variable is "inaccessible" from outside so "private"

Example 2: javascript classes

//private but is a good example
class includes {
    constructor(){}
  
    inculde_apps(file) { 
      var script  = document.createElement('script'); 
      script.src  = "ext/apps/"+file; 
      script.type = 'text/javascript'; 
      document.getElementsByTagName('body').item(0).appendChild(script);
    } 
    
    inculde_scripts(file) { 
      var script  = document.createElement('script'); 
      script.src  = "ext/scripts/"+file; 
      script.type = 'text/javascript'; 
      document.getElementsByTagName('body').item(0).appendChild(script);
    } 
    
    inculde_css(file) { 
        var script  = document.createElement('link'); 
        script.rel  = "stylesheet"; 
        script.type = 'text/css'; 
        script.href = "css/"+file;
        document.getElementsByTagName('head').item(0).appendChild(script);
    }
  }

Example 3: javascript classes

class SayHelloTo {
  name (to) {
    console.log(`Hello ${to}`);
  }
  constructor (to) {
    this.name(to);
  }
}
const helloWorld = new SayHelloTo(World);