๐class
๐ง under construction
JS โฉ value โฉ object โฉ class
(declaration) โญ๏ธ ES6 (2015)
creates a new class and gives it a name (identifier).
no commas between class member declarationsโ
private member identifiers are prefixed with hash (#).
// โญ๏ธ class definition
class A {
// โญ๏ธ instance members (# means "private")
publicProp; // this.publicProp = undefined (by default)
publicMethod() { ... } // this.publicMethod()
#privateProp; // this.#privateProp
#privateProp2 = 0;
get #privateGetter() { ... } // this.#privateGetter
#privateMethod() { ... } // this.#privateMethod()
// โญ๏ธ static members
static staticProp; // A.staticProp
// โญ๏ธ if omitted, a default (empty) constructor is created automatically.
constructor() { ... }
}
// โญ๏ธ class expression
let B = class { ... }
โญ class declaration is not hoistedโ
โ cannot use a class before its declaration โ(๐ temporal dead zone)
a class's prototype (property) is read-onlyโ
โญ๏ธโญ๏ธโญ๏ธ you can change the prototype of a function, but not that of a class.
// overwrite a function's prototype is OK
func.prototype = {} // โ
OK
// overwrite a class's prototype won't work
Class.prototype = {} // โ won't work (ignored silentlyโ๏ธ)
โ ReferenceError: cannot access '...' before initialization.
an error mostly caused by referencing a let/ const/ class in its temporal dead zoneโ
using typeof on lexical declaration (let/ const/ class) in its temporal dead zone will throw a ReferenceError.
// โญ temporal dead zone (start)
// ------------------------------
// typeof aLet; // โ ReferenceError: Cannot access 'aLet' before initialization
// typeof aConst; // โ ReferenceError: Cannot access 'aConst' before initialization
// typeof aClass; // โ ReferenceError: Cannot access 'aClass' before initialization
typeof undeclared; // โ 'undefined'
// โญ lexical declarations (let/const/class)
// --------------------------------------------
let aLet; // โญ initialized to `undefined`
const aConst = "hello";
class aClass {}
replit๏ผTDZ: let/const/class
What class User {...} does:
creates a function User, function body taken from constructor().
stores methods in User.prototype.
โญ๏ธ class is different from a regular function๏ผ
labelled by a special internal property
[[IsClassConstructor]]
.must be called with new.
methods are non-enumerable. (not enumerated in a for...in loop)
class body code always use strict.
โญ๏ธ ๆณจๆ๏ผclass ็่ชๆณ่็ดๆฅๅฏซไธๅ็ฉไปถ(literal object)็ๆนๅผไธๅใ
// โญ๏ธ property/method/getter/setter ้้ฝ่ฆ็จใ้่ใ้้โ๏ธ
let user = {
name : "John", // โญ๏ธ ๅฑฌๆง้็จใ้่ใ้้
surname: "Smith",
set fullName(value) {
[this.name, this.surname] = value.split(" ");
}, // โญ๏ธ method/getter/setter ้ไน่ฆ็จใ้่ใ้้
get fullName() {
return `${this.name} ${this.surname}`;
}
};
not block๏ผ
object literal / class / switch ( no block scopeโ)
function's scope is different from a block scopeโ
class identity
custom type name - define custom type name for class.
๐ฐ class examples
Complex - complex numbers.
other topics
custom functions
isClass() - check if value is a class.
โญ๏ธ class definition
class User {
// -------------------------
// โญ๏ธ static members
// -------------------------
static planet = "Earth"; // static property
static staticMethod() { // static method
return 'static method'
}
static get age() { return this._age } // getter
static set age(value) { // setter
if (value < 0) { return }
this._age = value
}
// -------------------------
// โญ๏ธ class fields
// -------------------------
age = 0; // regular class field
hobby = prompt('any hobby?'); // โญ๏ธ function value as property value
boundMethod = () => { }; // ๐ bound method (`this` === instance)
// -------------------------
// โญ๏ธ getters/setters
// -------------------------
get name() { return this._name } // getter
set name(value) { this._name = value } // setter
// -----------------
// โญ๏ธ methods
// -----------------
constructor() { } // โญ๏ธ constructor
method() { } // regular method
['say' + 'Hi']() { log(`hello`) } // โญ computed-name method
}
โญ๏ธ class expression
// โญ๏ธ class expression
let User2 = class {
sayHi() {}
};
// โญ๏ธ class expression as return value
function returnUserClass(){
return class {
sayHi() {}
};
}
Notes about Google Apps Script๏ผ
// โข (static/instance) "class fields".
// โ GAS ไธๆฏๆด
// โ
ๅฏ็จ (static/instance) getters/setters ไปฃๆฟ.
๐็ฏไพ๏ผ
let user = new User('Mary');
user.name = 'Joe'; // โญ setter: this._name = 'Joe'
user.sayHi(); // "hello"
User.age = -9; // "age cannot be negative: -9"
User.age = 10;
[
user.name, // "Joe"
user.hobby,
user.planet, // undefined
User.planet, // "Earth"
User.staticMethod(), // "static method"
User.age, // 10
].forEach(x => log(x));
JavaScript: The Definitive Guide โฉ 5.7.3 class
MDN โฉ JavaScript โฉ Classes
Last updated
Was this helpful?