CodingTrain / CodingTrain/Suggestion-Box
(Fake) Overloading in JS
- Dominant language
- No language data
- Stars
- 570
- Forks
- 85
- PR merge metrics
- No merged PRs in 30d
Description
- Function Overloading
- Operator Overloading
- "String" Overloading
- [I don't know what it's called] Overloading
## Function Overloading
Nah.
## Operator Overloading
OK, first, we have to understand the `valueOf()` function. `valueOf()` is a function, that typically performs on a string, that converts it into a "primitive" data type.
A number is a primitive data type, because it's not an object. And, you can see that by opening a Terminal window, running `node` and type:
```
> 2 + 2 // and get
4
```
Because JS doesn't have any "real" overloading, you can't put `+` in between two objects, in between two objects, and get an answer!
Well, with one slight exception, which is the `valueOf()` function. It will tell JS how to interpret an instance of an object as a primitive data type, whenever you want to add them together or something. If you put a `valueOf()` function into the prototype (or class, if you will) of an object, and it converts it into a number, or some other primitive data type, you can suddenly do arithmetic on these objects!
This does have a limitation, though. Say you write a class:
```javascript
class Vector {
constructor(x=0, y=0) {
this.x = x;
this.y = y;
}
// Maybe you want a valueOf() function to add them together
valueOf() {
// TL;DR What goes here???
}
}
```
Maybe you want to add the individual components of a vector to add two vectors together.
It turns out, the answer is, *nothing can go in the place of the question mark*!
Here's why: There's no real primitive data type, with further components. That's because that's what a primitive data type is! It doesn't have data, it *is it's own data!* A Vector does have further components, however, and so we arrive at a contradiction. Because a vector has further components, and `valueOf()` must return something without further components, this situation is impossible!
## String Overloading
Well, it's really the same as Operator Overloading, except you need to use the `toString()` function instead of the `valueOf()` function. It converts it into a string. I don't really know why you'd want to do this, maybe you would affect how you print it. I actually don't even know if `toString()` can do this.
## [TL;DR] Overloading
Look at this messy video about Matrix Math that you made: https://www.youtube.com/watch?v=NgZAIkDcPkI, at 23:15.
Guess what, there's a way to fix that!
I don't know how this one is called. When you say `this`, it's an object. But, because objects are associative arrays, you can actually treat `this` as an associative array! Something like this:
```javascript
class Matrix {
constructor(rows, cols) {
this.rows = rows;
this.cols = cols;
// this.data = []
for (let i = 0; i < this.rows; i++) {
this[i] = []; // OMG
for (let j = 0; j < this.cols; j++) {
this[i][j] = 0; // OMG
}
}
}
// .
// .
// .
}
```
It's as easy as that!
If you want to, you could even add a `this.length` variable, and suddenly, you would even be able to iterate over it, with a `for..of` loop!
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.