I have problems to understand classes, I think a new comparison will help.
Anonymous in /c/coding_help
807
report
I was struggling to understand classes in programming. When I was learning Python, I thought of classes as folders of functions. But that comparison didn’t help at all. It didn’t really feel like it was giving me a clear understanding of what classes are and when I should use them, and that was understandable, because a folder of functions isn’t really something you use much in real life, at least not me.<br><br>Then I started learning JavaScript. It has been a while since I learned about classes, but I’m back on that topic again. I’m confused, because JavaScript classes aren’t really classes, they’re just syntactic sugar over prototypes. But I don’t really care about that, I just want to understand “classes in general” (or how classes are understood in general), or at least how they should be used. <br><br>I started by assuming that classes are good for structuring things, and methods are good for when you need to use object properties to manipulate other things. Here are two examples that are examples for that (sorry for all the code, it’s kind of like my diary for classes, lol):<br><br>**Example 1**<br><br>Here I use a class to structure a shopping cart. I can just use the variable “cart” and have access to everything related to the cart, like the cart object, the cart methods, or even the cart array. But the cart object in the class gets all the methods, so it’s more compact. In addition, the methods use the cart to call other methods, like `cart.addItem()` or `cart.getTotal()`. So even if I decide to change the name of the methods, then I don’t have to rewrite all the methods, because they’re calling methods directly from the cart.<br><br>```javascript<br>function total(cart) {<br> let total = 0;<br> for (let i = 0; i < cart.length; i++) {<br> total += cart[i]['price'];<br> }<br> return total;<br>}<br><br>function addItem(cart, item) {<br> cart.push(item);<br>}<br><br>let cartObj = {<br> 'array': [],<br> 'add_item': addItem,<br> 'get_total': total<br>}<br><br>let cart = {<br> 'array': [],<br> 'add_item': function(){},<br> 'get_total': function(){},<br> 'calc_total': function(){},<br> 'calc_avg': function(){},<br> 'calc_smallest': function(){},<br> 'calc_largest': function(){},<br> 'calc_median': function(){},<br> 'classmethod': function(){<br> this.add_item({'name': 'car', 'price': 100000});<br> this.add_item({'name': 'house', 'price': 400000});<br> cart.add_item({'name': 'dog', 'price': 1000});<br> cart.add_item({'name': 'cat', 'price': 1500});<br> console.log('cart', this.array);<br> console.log('cart methods', this.array.length);<br> console.log('cart array length', this)<br> console.log('cart cart', cart)<br> }<br>}<br><br>cart.array = cartObj.array;<br>cart.add_item = cart.add_item.bind(cart, cartObj.array);<br>cart.get_total = cart.get_total.bind(cart, cartObj.array);<br><br>cart.calc_total = function() {<br> return total(cart.array);<br>};<br><br>cart.calc_avg = function() {<br> return total(cart.array) / cart.array.length;<br>}<br><br>cart.calc_smallest = function() {<br> let result = cart.array[0]['price'];<br> for (let i = 1; i < cart.array.length; i++) {<br> if (cart.array[i]['price'] < result) {<br> result = cart.array[i]['price'];<br> }<br> }<br> return result;<br>};<br><br>cart.calc_largest = function() {<br> let result = cart.array[0]['price'];<br> for (let i = 1; i < cart.array.length; i++) {<br> if (cart.array[i]['price'] > result) {<br> result = cart.array[i]['price'];<br> }<br> }<br> return result;<br>};<br><br>cart.calc_median = function() {<br> let result = cart.array[0]['price'];<br> let sorted = [cart.array[0]['price']];<br> for (let i = 1; i < cart.array.length; i++) {<br> let inserted = false;<br> for (let j = 0; j < sorted.length; j++) {<br> if (cart.array[i]['price'] > sorted[j]) {<br> sorted.splice(j, 0, cart.array[i]['price'])<br> inserted = true;<br> break;<br> }<br> }<br> if (inserted == false) {<br> sorted.push(cart.array[i]['price']);<br> }<br> }<br> return sorted[Math.floor(sorted.length / 2)];<br>};<br><br>cart.classmethod();<br>```<br><br>**Example 2**<br><br>Here I use a class to structure some constants for a game. I want to have them in a compact place, so I create a class with a bunch of methods. Here there is no data at all, just methods. I don’t need any data for any of the methods, I can just call them directly. This is useful because it’s almost like an enum. If I want to change the name of the methods, I don’t have to rewrite them all, because I’m calling them directly.<br><br>```javascript<br>let constants = {<br> 'type': 'enum',<br> 'values': ['apple', 'banana', 'orange']<br>}<br><br>let constantsClass = {<br> 'type': function(){return 'class'},<br> 'values_apple': function(){return 'apple'},<br> 'values_banana': function(){return 'banana'},<br> 'values_orange': function(){return 'orange'},<br> 'method': function() {<br> console.log('method', this)<br> console.log('method 1', this.values_apple())<br> console.log('method 2', this.values_banana())<br> console.log('method 3', this.values_orange())<br> }<br>}<br><br>constants.method = constantsClass.method.bind(constants, constants)<br>console.log(constants.type, constants.values)<br>constants.method();<br><br>constantsClass.method()<br>```<br><br>I get the feeling that I’m overcomplicating things, and I’m still not sure when to use classes vs when not to use them. Is there a better comparison than just “folders of functions”, or are there any other use cases that I’m not thinking of?
Comments (16) 24633 👁️