get in javascript class code example
Example 1: javascript get class name
document.getElementById("myDIV").className = "mystyle";
Example 2: js get class property
let element = document.querySelector('.preset');
let style = getComputedStyle(element);
let bg = style.backgroundColor;
Example 3: javascript class getter setter
class Person {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
get fullName() {
return `Hello ${this.firstname} ${this.lastname}`;
}
set fullName(space) {
const parts = space.split(' ');
this.firstname = parts[0];
this.lastname = parts[1];
}
}
let run = document.getElementById("run");
run.addEventListener('click', () => {
let john = new Person('John', 'Connor');
console.log(john.fullName);
john.fullName = 'Billy Butcher';
console.log(john.firstname + ' ' + john.lastname);
})
Example 4: get methods of class javascript
class Hi {
constructor() {
console.log("hi");
}
my_method(){}
static my_static_method() {}
}
function getMethods(cl) {
return Object.getOwnPropertyNames(cl.prototype)
}
console.log(getMethods(Hi))