Cet article décrit la méthode de simulation de la table JS. Partagez-le pour votre référence. Les méthodes spécifiques sont les suivantes:
<! doctype html> <html> <éad- head> <meta charset = "utf-8"> <tight> list </ title> <script> function list () {this.listSize = 0; this.pos = 0; this.datastore = []; // Initialisez un tableau vide pour enregistrer l'élément de liste this.clear = clear; this.find = find; this.toString = toString; this.insert = insert; this.append = append; this.reMove = retirer; this.front = front; this.end = end; this.prev = prev; this.next = suivant; this.length = longueur; this.currPos = currPos; this.moveto = moveto; this.getElement = getElement; this.length = longueur; this.Contains = contient; Fonction append (élément) {this.datastore [this.listSize ++] = élément; }; fonction find (élément) {for (var i = 0; i <this.datastore.length; i ++) {if (this.datastore [i] === élément) {return i; }} return -1; } fonction supprime (élément) {var fondeat = this.find (élément); if (Foundat! = - 1) {this.datastore.splice (Foundat, 1); this.listSize--; élément de retour; // return true C'était à l'origine comme ça, je l'ai changé en élément} else {return false; }} fonction de fonction () {return this.listSize; } fonction toString () {return this.datastore; } fonction insert (élément, après) {var insertPos = this.find (après); if (insertPos! = - 1) {this.datastore.splice (insertpos + 1,0, élément); this.listSize ++; Retour Vrai; } else {return false; }} fonction clear () {supprimer this.datastore; this.listSize = 0; this.pos = 0; this.datastore = []; } fonction contient (élément) {for (var i = 0; i <this.datastore.length; i ++) {if (this.datastore [i] === élément) {return true; }} return false; } fonction front () {this.pos = 0; } function end () {this.pos = this.listSize-1; } fonction prev () {// if (this.pos> 0) {--this.pos; //}} fonction suivant () {// if (this.pos <this.listSize-1) {++ this.pos; //}} fonction currpos () {return this.pos; } fonction moveto (pos) {this.pos = pos; } fonction getElement () {return this.datastore [this.pos]; }} var list = new list (); list.append ({name: 'xia guangcheng', sexe: 'mâle'}); list.append ({name: 'jiang rongsheng', sexe: 'mâle'}); list.append ({name: 'du qiang', sexe: 'mâle'}); list.append ({name: 'qiaohua', sexe: 'femelle'}); list.append ({name: 'fang yang', sexe: 'mâle'}); list.append ({name: 'fang yang', sexe: 'mâle'}); for (list.front (); list.currPos () <list.length (); list.next ()) {var item = list.getElement (); if (item.sex == 'feme') {list.Remove (item); }} pour (list.front (); list.currPos () <list.length (); list.next ()) {var item = list.getElement (); console.log (item.name); } </ script> </ head> <body> </ body> </html>J'espère que cet article sera utile à la programmation JavaScript de tous.