Getter is not defined | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Getter is not defined

I don’t quite understand why getter is not defined. Maybe i need do it with the help "Object.defineProperty"? (program logic: a random number from 1 to 3 is taken. Using the object, a conforming messages are outputs and the "eventListener" get the object) https://code.sololearn.com/WU0CPb2PtGLI/?ref=app

1st Sep 2019, 10:52 AM
Chill Dealer
Chill Dealer - avatar
1 Answer
0
A few things! Getters can only be defined as part if an object or a class, like so let obj = { get four () { return 4; } }; class C { get four () { return 4; } } You can also do everything with Object.defineProperty but it is worth looking into classes. Regarding design, I would create a single Counter class and make three counter objects rather than a single counter object that handles three values, it makes for less code. And also I think a getter is not appropriate here. Getters are for returning values and here you are performing an action (some DOM manipulation) and not returning anything. But I understand you are just checking things out so it's fine. class Counter { constructor (x, node) { this.value = x; this.node = node; } get count () { return this.value; } update () { this.node.innerHTML = this.count; } } let c1 = new Counter(1, document.getElementById("fullno1"));
1st Sep 2019, 11:09 AM
Schindlabua
Schindlabua - avatar