๐ธmember
๐ง under construction
JS โฉ value โฉ object โฉ class โฉ member
const { log } = console;
// โญ๏ธ class definition
// --------------------------------------------------------
// โ GAS doesn't support (static/instance) "class fields".
// โ
GAS does support "static methods/getters/setters".
class User {
// -------------------------
// โญ๏ธ static members
// -------------------------
// โ GAS ไธๆฏๆด (static) "class fields".
// โ
ๅฏ็จ static getters/setters ไปฃๆฟ.
static planet = "Earth"; // static property
// โ
GAS ๆฏๆด "(static) methods/getters/setters".
static staticMethod() { // static method
return 'static method'
}
// -----------------------------------------------------
// โญ๏ธ static getters/setters as static properties
// -----------------------------------------------------
// โ
GAS ๅฏ็จ้ๅไพไปฃๆฟ static class fields.
static get age() { return this._age } // getter
static set age(value) { // setter
if (value < 0) { log(`age cannot be negative: ${value}`); return; }
this._age = value
}
// -------------------------
// โญ๏ธ class fields
// -------------------------
// โ GAS ไธๆฏๆด "class fields".
// โ
ๅฏ็จ getters/setters ไปฃๆฟ.
age = 0; // regular class field
hobby = prompt('any hobby?', "game"); // โญ๏ธ function value as property value
boundMethod = () => { }; // ๐ bound method (`this` === instance)
// -------------------------
// โญ๏ธ getters/setters
// -------------------------
// โ
GAS ๅฏ็จ้ๅไพไปฃๆฟ instace class fields.
get name() { return this._name } // getter
set name(value) { // setter
this._name = value
}
// -----------------
// โญ๏ธ methods
// -----------------
constructor() { } // โญ๏ธ constructor
method() { } // regular method
['say' + 'Hi']() { log(`hello`) } // โญ computed-name method
}
// test code
let user = new User('Mary');
user.name = 'Joe'; // โญ setter: this._name = 'Joe'
user.sayHi(); // "hello"
// test static getter/setter
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));
// โญ๏ธ class expression
// ---------------------------------------------------------------
let User2 = class {
sayHi() {}
};
// โญ๏ธ class expression as return value
// ---------------------------------------------------------------
function returnUserClass(){
return class {
sayHi() {}
};
}
class ๅฎ็พฉ่ๅพๆๅนพๅๅ่งฃๅไฝ๏ผ
class ๆฌ่บซๆฏๅ Function (ๆ prototype)
static members ๆพๅฐ class ๆฌ่บซ
instance methods (ๅซ constructor)๏ผๆพๅฐ prototypeใ
constructor() ๅ ง็ this.prop ๅฑฌๆงๆพๅฐ instanceใ
็บ class ่ทๅฎ็ prototype ่จญๅฎ prototype chain (__proto__ ๅฑฌๆง)๏ผๅฆไธ่กจ๏ผ
object prototype chain (.__proto__)
-----------------------------------------
Animal Function.prototype
Rabbit Animal.prototype
Animal.prototype Object.prototype
Rabbit.prototype Animal.prototype
ไนๅฐฑๆฏ่ชช๏ผ้้ class ่ชๆณๅฎฃๅ็ Animal๏ผๅ ถๅฏฆๆฏๅฎฃๅไบไธๅ function Animal๏ผ่ไธๅ Animal.prototype (ๅจ class body ๅ งๅฎ็พฉ็ๆนๆณ้ฝๆพๅจ้่ฃก)ใ
JS.info โฉ Classes
Last updated