Artikel ini menjelaskan metode simulasi tabel JS. Bagikan untuk referensi Anda. Metode spesifiknya adalah sebagai berikut:
<! Doctype html> <html> <head> <meta charset = "utf-8"> <itement> daftar </iteme> <script> daftar fungsi () {this.listsize = 0; this.pos = 0; this.datastore = []; // inisialisasi array kosong untuk menyimpan elemen daftar this.clear = clear; this.find = find; this.toString = tostring; this.insert = masukkan; this.append = append; this.remove = hapus; this.front = depan; this.end = end; this.prev = prev; this.next = next; this.length = panjang; this.currpos = Currpos; this.moveto = moveto; this.getElement = GetElement; this.length = panjang; this.contains = berisi; function append (elemen) {this.datastore [this.listSize ++] = elemen; }; function find (element) {for (var i = 0; i <this.datastore.length; i ++) {if (this.datastore [i] === elemen) {return i; }} return -1; } fungsi hapus (elemen) {var foundat = this.find (elemen); if (foundat! =-1) {this.datastore.splice (foundat, 1); this.listSize--; elemen kembali; // return true itu awalnya seperti ini, saya mengubahnya menjadi elemen} else {return false; }} panjang fungsi () {return this.listSize; } function toString () {return this.datastore; } function insert (elemen, setelah) {var insertPos = this.find (setelah); if (insertPos! =-1) {this.datastore.splice (insertPos+1,0, elemen); this.listSize ++; Kembali Benar; } else {return false; }} function clear () {hapus this.datastore; this.listSize = 0; this.pos = 0; this.datastore = []; } fungsi berisi (elemen) {untuk (var i = 0; i <this.datastore.length; i ++) {if (this.datastore [i] === elemen) {return true; }} return false; } function front () {this.pos = 0; } function end () {this.pos = this.lissize-1; } function prev () {// if (this.pos> 0) {--this.pos; //}} function next () {// if (this.pos <this.lissize-1) {++ this.pos; //}} function Currpos () {return this.pos; } function moveto (pos) {this.pos = pos; } function getElement () {return this.datastore [this.pos]; }} var list = new list (); list.append ({name: 'xia guangcheng', seks: 'laki -laki'}); list.append ({name: 'jiang rongsheng', seks: 'laki -laki'}); list.append ({name: 'du qiang', sex: 'Male'}); list.append ({name: 'qiaohua', sex: 'female'}); list.append ({name: 'fang Yang', sex: 'Male'}); list.append ({name: 'fang Yang', sex: 'Male'}); untuk (list.front (); list.currpos () <list.length (); list.next ()) {var item = list.getElement (); if (item.sex == 'betina') {list.remove (item); }} untuk (list.front (); list.currpos () <list.length (); list.next ()) {var item = list.getElement (); console.log (item.name); } </script> </head> <body> </body> </html>Saya harap artikel ini akan membantu pemrograman JavaScript semua orang.