Skip to content

Latest commit

 

History

History
57 lines (45 loc) · 729 Bytes

static-private-semantic.md

File metadata and controls

57 lines (45 loc) · 729 Bytes

Issue of static private

const secret = '...'
/*
...
*/
class Base {
	static login() {
		secret
	}
}
class Sub extends Base {
	static doSth() {
		this.login()
	}
}
Sub.doSth()

Refactory for Cohesion

class Base {
	static #secret = '...'
	static login() {
		this.#secret
	}
}
class Sub extends Base {
	static doSth() {
		this.login()
	}
}
Sub.doSth()

Throw!

Prototype!

Possible Solution

New ESLint rule: no-static-this-ref-private

  • Disallow this.#foo in static methods
  • Disallow this.constructor.#foo in instance methods
  • Enforce class.#foo in all methods (but only stage 1 now)

Simple way

Use symbol-based semantic instead of weakmap-based

See comparison of different private solutions