Last active
January 11, 2021 00:29
-
-
Save the-liquid-metal/41b2bcf98a779ad21953f1e5546f7e8a to your computer and use it in GitHub Desktop.
private fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
constructor() { | |
console.log("A constructor is called"); | |
} | |
// --------------------------------------------- | |
#prop1; | |
set prop1(val) { | |
this.#prop1 = val; | |
} | |
get prop1() { | |
return this.#prop1; | |
} | |
check1() { | |
console.log("a2: " + this.#prop1); | |
} | |
// --------------------------------------------- | |
#prop2; | |
prop2 = { | |
set(val) {this.#prop2 = val}, | |
get() {return this.#prop2}, | |
}; | |
check2() { | |
console.log("a3: " + this.#prop2); | |
} | |
// --------------------------------------------- | |
propEmpty; | |
} | |
class B extends A {} | |
let a, b; | |
// ============================== | |
console.log("CHECK prop1"); | |
a = new B(); | |
a.prop1 = 30; | |
console.log(a.prop1); | |
a.check1(); | |
a.prop1 = 35; | |
console.log(a.prop1); | |
a.check1(); | |
b = new B(); | |
b.prop1 = 40; | |
console.log(b.prop1); | |
b.check1(); | |
b = new B(); | |
b.prop1 = 45; | |
console.log(b.prop1); | |
b.check1(); | |
// ============================== | |
console.log("CHECK prop2"); | |
a = new B(); | |
a.prop2 = 50; | |
console.log(a.prop2); | |
a.check2(); | |
a.prop2 = 55; | |
console.log(a.prop2); | |
a.check2(); | |
// ============================== | |
console.log("a:", a); | |
// ============================== | |
console.log("CHECK propEmpty"); | |
console.log("a.propEmpty: ", a.propEmpty); | |
console.log("a.propEmpty == null: ", a.propEmpty == null); | |
console.log("a.propEmpty == false: ", a.propEmpty == false); | |
console.log("a.propEmpty == '': ", a.propEmpty == ''); | |
console.log("a.propEmpty == 0: ", a.propEmpty == 0); | |
console.log("a.propEmpty == {}: ", a.propEmpty == {}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A = class { | |
prop1 = {x: 10}; | |
prop2 = [1,2,3]; | |
} | |
o1 = new A(); | |
o2 = new A(); | |
o1.prop1.x = 1000; | |
console.log("o1.prop1.x", o1.prop1.x); | |
console.log("o2.prop1.x", o2.prop1.x); | |
o2.prop2.push(4,5,6); | |
console.log("o1.prop2", o1.prop2); | |
console.log("o2.prop2", o2.prop2); | |
// ========================================================= | |
B = class { | |
#prop1 = 11; | |
#prop2 = 22; | |
#prop3 = 33; | |
#prop4 = 44; | |
constructor() { | |
this.#prop1 = 55; | |
this.#prop2 = 66; | |
} | |
getProp1() { | |
console.log("(B) this.#prop1", this.#prop1); | |
} | |
getProp2() { | |
console.log("(B) this.#prop2", this.#prop2); | |
} | |
getProp3() { | |
console.log("(B) this.#prop3", this.#prop3); | |
} | |
getProp4() { | |
console.log("(B) this.#prop4", this.#prop4); | |
} | |
} | |
C = class extends B { | |
#prop1 = 77; | |
#prop2 = 88; | |
#prop3 = 99; | |
#prop4; | |
constructor() { | |
super(); | |
super.call(this); | |
this.#prop1 = 11111; | |
} | |
getProp1() { | |
console.log("(C) this.#prop1", this.#prop1); | |
} | |
getProp2() { | |
console.log("(C) this.#prop2", this.#prop2); | |
} | |
getProp3() { | |
console.log("(C) this.#prop3", this.#prop3); | |
} | |
getProp4() { | |
console.log("(C) this.#prop4", this.#prop4); | |
} | |
} | |
o1 = new B(); | |
o1.getProp1(); | |
o1.getProp2(); | |
o1.getProp3(); | |
o1.getProp4(); | |
o2 = new C(); | |
o2.getProp1(); | |
o2.getProp2(); | |
o2.getProp3(); | |
o2.getProp4(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class a { | |
static #_sp1 = 20; | |
static set #sp1(v1) {this.#_sp1 = v1} | |
static get #sp1() {return this.#_sp1} | |
static #sm1() {this.#sp1 = 10; console.log(this.#sp1)} | |
static _sp2 = 20; | |
static set sp2(v2) {this._sp2 = v2} | |
static get sp2() {return this._sp2} | |
static sm2() {this.sp2 = 10; console.log(this.sp2)} | |
#_ip1 = 10; | |
set #ip1(v1) {this.#_ip1 = v1} | |
get #ip1() {return this.#_ip1} | |
#im1() {this.#ip1 = 10; console.log(this.#ip1)}; | |
_ip2 = 10; | |
set ip2(v1) {this._ip2 = v1} | |
get ip2() {return this._ip2} | |
im2() {this.ip2 = 10; console.log(this.ip2)}; | |
} | |
const i = new a (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment