pybuiltinfunc
Version:
Python built-in Functions and Modules in Javascript
57 lines (56 loc) • 978 B
JavaScript
class List {
constructor(value = []) {
this.value = value;
}
append(item) {
this.value.push(item);
}
insert(num = 0, item = "") {
this.value[num] = item;
}
extend(newList = []) {
let i;
for (i in newList) {
this.value.push(newList[i]);
i++;
}
}
remove(item) {
const index = this.value.indexOf(item);
if (index > -1) {
this.value.splice(index, 1);
}
}
pop(num) {
if (!num) {
this.value.pop();
} else {
this.value.splice(num, 1);
}
}
clear() {
this.value = [];
}
reverse() {
this.value.reverse();
}
count(x) {
let count = 0;
this.value.forEach((item) => {
if (item === x) {
count++;
}
});
return count;
}
copy() {
return this.value.slice();
}
sort() {
return this.value.sort();
}
return() {
return this.value;
}
}
module.exports.List = List;