function classes javascript code example
Example 1: javascript new function as class
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();
Example 2: javascript create class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
present = () => {
console.log(`Hi! i'm ${this.name} and i'm ${this.age} years old`)
}
}
let me = new Person("tsuy", 15);
me.present();
Example 3: javascript classes
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 4: class declaration in javascript
class NameOfClass {
obj="text";
obj2="some other text";
}
console.log(new NameOfClass);