Javascript prototype function like Kotlin extensions

42 Views Asked by At

I wonder if Javascript Prototype can extend class like Kotlin Extension does. Example:

fun MutableList<Int>.swap(index1: Int, index2: Int) {
    val tmp = this[index1] // 'this' corresponds to the list
    this[index1] = this[index2]
    this[index2] = tmp
}

we can receive the object using this keyword, but I can't figure out how to do it in JavaScript.

I have tried this, but I'm out of clue how can I receive the object? It's just giving me Windows object.

Map.prototype.isValid = () => {
    return this
}

Thanks in advance

(edited) I already read this solution which is so different with my case. I'm trying to extend Map class which is standard built-it objects.

0

There are 0 best solutions below