Hello, thank you for watching. Please forgive me for the shortcomings in the following content. I would like to share a discussion about the syntax of the inherited object keyword. The pseudo code is as follows:
object Parent {
name = 'parent'
echoName(){
console.log(this.name)
}
}
object Child extends Parent{
name = 'child'
}
Parent.echoName() //parent
Child.echoName() //child
The following is an explanation of the source of this proposal idea. I often use static methods in program development. Compared with ordinary functions, static methods have better static prompts and module distinction functions. However, I encountered a problem, that is, static methods cannot be inherited and rewritten. This is a unified design specification in the language I am familiar with. However, in the development process, this does lead to inconvenience. There is a website called Zhihu in China about [Why not use all static methods in Java? 】, this topic has caused widespread discussion. Some people think that using all static syntax is not enough for OOP, while others think that this method does bring enough efficiency improvement to development and improves the development experience.
Many people have encountered the same problem as me, that is, when using static development, abstract parent classes are needed. When methods are reused, static cannot be rewritten, which reduces development efficiency.
However, due to the special logic of static, it seems inappropriate to support rewriting of static methods.
So can a new keyword, object, be supported to implement this function?
Object has the function of class, can be integrated by class, can be inherited by object, and can be used directly.
Regarding OOP content, there are more practical applications in Java and Kotlin, so please allow me to use Java as an example to illustrate. In Java In web development, the spring framework is an unavoidable framework. Object instances managed by spring are all singletons, which means that a class will only have one instance. This is the case in most cases. However, in this case, we need to declare a class first, and then instantiate this class to get the object. This process is a bit cumbersome. We can merge these two steps into one step, that is, declare an object with type hints and use it immediately.
In kotlin, there is the object keyword syntax. The following code can be run in kotlin
object ParentObject {
var name = "ParentObject"
fun echoName() = "name:${name}"
}
// Use
fun main() {
println(ParentObject.echoName()) // Output: ParentObject
}
And kotlin's object supports inheritance from class, but it does not support inheritance from object, which still cannot solve the problem I encountered at the beginning
So I would like to ask if it is possible to support this function in js
Looking forward to your answer, thank you very much