JS表的模擬方法
更新時間:2015年02月05日 15:32:40 作者:magicfly9527
這篇文章主要介紹了JS表的模擬方法,涉及javascript模擬表的生成、添加與刪除節(jié)點的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JS表的模擬方法。分享給大家供大家參考。具體方法如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>列表</title> <script> function List(){ this.listSize= 0; this.pos=0; this.dataStore=[];//初始化一個空數(shù)組來保存列表元素 this.clear =clear; this.find = find; this.toString= toString; this.insert = insert; this.append= append; this.remove =remove; this.front = front; this.end = end; this.prev =prev; this.next = next; this.length= length; this.currPos= currPos; this.moveTo= moveTo; this.getElement =getElement; this.length = length; this.contains = contains; function append(element){ this.dataStore[this.listSize++] = element; }; function find(element){ for(var i =0; i<this.dataStore.length; i++){ if(this.dataStore[i]===element){ return i ; } } return -1; } function remove(element){ var foundAt =this.find(element); if(foundAt!=-1){ this.dataStore.splice(foundAt,1); this.listSize--; return element; //return true 原本是這樣,后面本人改為ELEMENT }else{ return false; } } function length(){ return this.listSize; } function toString(){ return this.dataStore; } function insert(element,after){ var insertPos = this.find(after); if(insertPos!=-1){ this.dataStore.splice(insertPos+1,0,element); this.listSize++; return true; }else{ return false; } } function clear (){ delete this.dataStore; this.listSize=0; this.pos=0; this.dataStore=[]; } function contains(element){ for(var i =0; i<this.dataStore.length; i++){ if(this.dataStore[i]===element){ return true ; } } return false; } function front(){ this.pos = 0; } function end(){ this.pos = this.listSize-1; } function prev(){ // if(this.pos> 0){ --this.pos; // } } function next(){ // if(this.pos< this.listSize-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:'夏廣成',sex:'男'}); list.append({name:'江榮盛',sex:'男'}); list.append({name:'杜強',sex:'男'}); list.append({name:'巧華',sex:'女'}); list.append({name:'方陽',sex:'男'}); for(list.front(); list.currPos() < list.length() ; list.next()){ var item = list.getElement(); if(item.sex =='女'){ list.remove(item); } } for(list.front(); list.currPos() < list.length() ; list.next()){ var item = list.getElement(); console.log(item.name); } </script> </head> <body > </body> </html>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
原生JS實現(xiàn)動態(tài)添加新元素、刪除元素方法
這篇文章主要介紹了原生js實現(xiàn)動態(tài)添加新元素、刪除元素方法 ,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05JavaScript來實現(xiàn)打開鏈接頁面的簡單實例
下面小編就為大家?guī)硪黄狫avaScript來實現(xiàn)打開鏈接頁面的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06Echarts直角坐標(biāo)系x軸y軸屬性設(shè)置整理大全
直角坐標(biāo)系的設(shè)置指的是網(wǎng)格,坐標(biāo)軸和區(qū)域縮放的配置,下面這篇文章主要給大家介紹了關(guān)于Echarts直角坐標(biāo)系x軸y軸屬性設(shè)置的相關(guān)資料,需要的朋友可以參考下2022-11-11