Open hon9g opened 5 years ago
Define Class from Representation
class Cat {
// Constructors and Properties
constructor(name, color) {
this.name = name
this.color = color
}
// Methods
speak() {
return`${this.name} is Mewwww`
}
}
Create new
Instance
var cat = new Cat('Fluffy', 'White')
console.log(cat); // Cat {name: "Fluffy", color: "White"} console.log(cat.speak()); // Fluffy is Mewwww
- `type of` class Cat
```JavaScript
console.log(typeof Cat); // function
type of
instance cat
console.log(typeof cat); // object
instanceof
console.log(cat instanceof Cat);
class Cat {
constructor(name, color) {
this.name = name
this.color = color
}
Cat.maxHeight = 500; // Static property
}
class Cat {
constructor(name, color) {
this.name = name
this.color = color
}
static feed() {
console.log('yum yum');
}
}
let cat = new Cat('nabi', 'yellow');
console.log(Cat.feed()); // yum yum
console.log(cat.feed()); // Uncaught TypeError: nabi.feed is not a function
class Employee {
constructor(id) {
this._id = id;
}
// This is the getter
get id() {
return this._id;
}
}
let h = new Employee(9999);
method id()
like a property
console.log(h.id); //9999
class Employee {
constructor(id) {
this._id = id;
}
get id() {
return this._id;
}
// This is the setter
set id(value) {
this._id = value;
}
}
let h = new Employee(9999);
h.id = 7777;
console.log(h.id); //7777
INDEX