In a class
, how do I access its base-class's private field, say #property
?
class Base { #property = '1.618' toString() { return Base.name } } class X extends Base { thisWorks() { return super.toString() } toString() { return super.#property // SyntaxError: Unexpected private field } } console.log(`${new X}`)
In OOP you can not access private
method or property outside of the class even when you extend. But you can access protected
method of parent class in child class.
It is impossible :
It means that private fields are purely internal: no JS code outside of a class can detect or affect the existence, name, or value of any private field of instances of said class without directly inspecting the class's source, unless the class chooses to reveal them. ( This includes subclasses and superclasses.)
Base
would have to deliberately expose its #property
in some other way, like through a method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.