Класи
Клас визначає, яким буде об'єкт і що він буде робити
Типізація - динамічна (можна передавати, і цифру, і рядок, і об'єкт у метод)
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
class Test {
constructor(size) { // початкове значення size - undefined
this.size = size;
}
get Show() {
return this._name + "<br>";
}
}
var t = new Test(7);
document.write("<h1>"+t.size+"</h1>"); // 7 (t["size"])
Статичні поля і методи
class Test{
constructor() {
Test.count++;
}
static Default(){
alert("Ok");
}
}
Test.count = 0;
Test.Default();
Функція-конструктор
function Worker(name){
this.name = name;
this.workerMoney = 0;
this.getWorkerMoney = function(value){
workerMoney = value;
}
}
var w1 = new Worker("Bohdan");
Get і set - властивості без ()
class Worker {
constructor(n) {
this._name = n;
}
get name() {
return this._name;
}
set name(x) {
this._name = x;
}
}
var w1 = new Car("Bohdan");
d1.innerHTML = w1.name;