تصف هذه المقالة طريقة محاكاة جدول JS. شاركه للرجوع إليه. الطرق المحددة هي كما يلي:
<! doctype html> <html> <head> <meta charset = "utf-8"> <title> قائمة </title> <script> قائمة الوظائف () {this.listize = 0 ؛ this.pos = 0 ؛ this.datastore = [] ؛ // تهيئة صفيف فارغ لحفظ عنصر القائمة this.clear = clear ؛ this.find = find ؛ this.toString = toString ؛ this.insert = 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 = يحتوي على ؛ دالة إلحاق (element) {this.datastore [this.listize ++] = element ؛ } ؛ Function Find (element) {for (var i = 0 ؛ i <this.datastore.length ؛ i ++) {if (this.datastore [i] === element) {return i ؛ }} return -1 ؛ } وظيفة إزالة (element) {var foundat = this.find (element) ؛ if (foundat! =-1) {this.datastore.splice (foundat ، 1) ؛ this.listize-- ؛ عنصر العودة ؛ // return true كان الأمر مثل هذا في الأصل ، لقد قمت بتغييره إلى element} آخر {return false ؛ }} طول الوظيفة () {return this.listize ؛ } وظيفة toString () {return this.datastore ؛ } وظيفة إدراج (عنصر ، بعد) {var insertpos = this.find (بعد) ؛ if (insertpos! =-1) {this.datastore.splice (insertpos+1،0 ، element) ؛ this.listizeize ++ ؛ العودة صحيح. } آخر {return false ؛ }} function clear () {delete this.datastore ؛ this.listize = 0 ؛ this.pos = 0 ؛ this.datastore = [] ؛ } الدالة تحتوي على (element) {for (var i = 0 ؛ i <this.datastore.length ؛ i ++) {if (this.datastore [i] === element) {return true ؛ }} إرجاع خطأ ؛ } وظيفة Front () {this.pos = 0 ؛ } الوظيفة end () {this.pos = this.listize-1 ؛ } الدالة prev () {// if (this.pos> 0) { -this.pos ؛ //}} الدالة next () {// if (this.pos <this.listize-1) {++ this.pos ؛ //}} دالة currpos () {return this.pos ؛ } وظيفة moveto (pos) {this.pos = pos ؛ } وظيفة getElement () {return this.datastore [this.pos] ؛ }} var list = new list () ؛ list.append ({name: 'xia guangcheng' ، sex: 'male'}) ؛ list.append ({name: 'Jiang Rongsheng' ، sex: 'male'}) ؛ 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'}) ؛ لـ (list.front () ؛ list.currpos () <list.length () ؛ list.next ()) {var item = list.getElement () ؛ if (item.sex == 'female') {list.remove (item) ؛ }} لـ (list.front () ؛ list.currpos () <list.length () ؛ list.next ()) {var item = list.getElement () ؛ console.log (item.name) ؛ } </script> </head> <body> </body> </html>آمل أن تكون هذه المقالة مفيدة لبرمجة JavaScript للجميع.