บทความนี้อธิบายวิธีการจำลองของตาราง JS แบ่งปันสำหรับการอ้างอิงของคุณ วิธีการเฉพาะมีดังนี้:
<! doctype html> <html> <head> <meta charset = "utf-8"> <title> รายการ </title> <title> <script> รายการฟังก์ชัน () {this.listisize = 0; this.pos = 0; this.datastore = []; // เริ่มต้นอาร์เรย์ที่ว่างเปล่าเพื่อบันทึกองค์ประกอบรายการ this.clear = clear; this.find = ค้นหา; this.toString = toString; this.insert = แทรก; this.append = ผนวก; this.remove = ลบ; this.front = ด้านหน้า; this.end = สิ้นสุด; this.prev = prev; this.next = ถัดไป; this.length = ความยาว; this.currpos = currpos; this.moveto = moveto; this.getElement = GetElement; this.length = ความยาว; this.contains = มี; ภาคผนวกฟังก์ชั่น (องค์ประกอบ) {this.datastore [this.listsize ++] = องค์ประกอบ; - ฟังก์ชั่นค้นหา (องค์ประกอบ) {สำหรับ (var i = 0; i <this.datastore.length; i ++) {ถ้า (this.datastore [i] === องค์ประกอบ) {return i; }} return -1; } ฟังก์ชันลบ (องค์ประกอบ) {var foundat = this.find (องค์ประกอบ); if (foundat! =-1) {this.datastore.splice (foundat, 1); สิ่งนี้ออกมา-; องค์ประกอบกลับ; // return true แต่เดิมฉันเปลี่ยนเป็นองค์ประกอบ} else {return false; }} ความยาวของฟังก์ชัน () {return this.listsize; } function toString () {return this.datastore; } ฟังก์ชันแทรก (องค์ประกอบ, หลังจาก) {var insertPos = this.find (หลังจาก); if (insertPos! =-1) {this.datastore.splice (insertPos+1,0, องค์ประกอบ); this.listsize ++; กลับมาจริง; } else {return false; }} ฟังก์ชั่น Clear () {ลบ this.datastore; this.listsize = 0; this.pos = 0; this.datastore = []; } ฟังก์ชั่นมี (องค์ประกอบ) {สำหรับ (var i = 0; i <this.datastore.length; i ++) {ถ้า (this.datastore [i] === องค์ประกอบ) {return true; }} return false; } function front () {this.pos = 0; } function End () {this.pos = this.listsize -1; } ฟังก์ชันก่อนหน้า () {// ถ้า (this.pos> 0) { -this.pos; //}} ฟังก์ชั่น next () {// ถ้า (this.pos <this.listisize -1) {++ this.pos; //}} ฟังก์ชั่น currpos () {return this.pos; } ฟังก์ชั่น moveto (pos) {this.pos = pos; } ฟังก์ชัน GetElement () {return this.datastore [this.pos]; }} var list = new list (); list.append ({ชื่อ: 'Xia Guangcheng', Sex: 'Male'}); list.append ({ชื่อ: 'Jiang Rongsheng', Sex: 'Male'}); list.append ({ชื่อ: 'du Qiang', เพศ: 'ชาย'}); list.append ({ชื่อ: 'Qiaohua', เพศ: 'หญิง'}); list.append ({ชื่อ: 'Fang Yang', Sex: 'Male'}); list.append ({ชื่อ: 'Fang Yang', Sex: 'Male'}); สำหรับ (list.front (); list.currpos () <list.length (); list.next ()) {var item = list.getElement (); if (item.sex == 'female') {list.remove (รายการ); }} สำหรับ (list.front (); list.currpos () <list.length (); list.next ()) {var item = list.getElement (); console.log (item.name); } </script> </head> <body> </body> </html>ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน