I recently started learning JS, main reason - want to learn node and related frameworks. I have started doing some basic tutorials from freecodecamp and referring the docs from Mozilla MDN
I have started with ES6 constructor functions and stuck with something. Below is the details:
class Book {
constructor(author) {
this._author = author;
}
// getter
get writer() {
return this._author;
}
// setter
set writer(updatedAuthor) {
this._author = updatedAuthor;
}
}
The invocation of getter and setter is weird, coming from Java, but its okay. Adapt !!
const ALEPH = new Book("Paulo Coelho");
let writtenBy = ALEPH.writer;
ALEPH.writer = "So we changing Authors now !!"
The above code(syntax) makes sense when thinking in terms of :
const person = {
name: "Taylor",
sayHello() {
return `Hello! My name is ${this.name}.`;
}
};
And that class is not a real class form OOP concepts, but a template for something called - constructor functions.
It should be noted that the class syntax is just syntax, and not a full-fledged class-based implementation of an object-oriented paradigm, unlike in languages such as Java, Python, Ruby, etc.
This makes sense, how the getters and setters are being called as they are nothing but keys/attributes of the object holding a function against it.
Please correct me if my understanding is wrong or I need to be more technically correct
The question is, I have looking over the internet what the get and set keyword does internally.
I have found implementations and how to use them, not what they doing internally.
- If you can refer me to any docs which talks about what it does internally.
With Java, I can easily refer the internal implementation or in case of a keyword, look up the Java Spec or JVM spec to understand what is happening under the hood. I am looking for something similar for JS. Would be good to have a reference to such official docs.
Question:
- How are the get and set keyword implemented internally?
- Any official docs for JS like we have for Python or Java.
Thanks.