JS表的模擬方法
本文實(shí)例講述了JS表的模擬方法。分享給大家供大家參考。具體方法如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>列表</title> <script> function List(){ this.listSize= 0; this.pos=0; this.dataStore=[];//初始化一個(gè)空數(shù)組來(lái)保存列表元素 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:'杜強(qiáng)',sex:'男'}); list.append({name:'巧華',sex:'女'}); list.append({name:'方陽(yáng)',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>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
javascript單頁(yè)面手勢(shì)滑屏切換原理詳解
這篇文章主要為大家詳細(xì)介紹了javascript單頁(yè)面手勢(shì)滑屏切換原理,感興趣的小伙伴們可以參考一下2016-03-03原生JS實(shí)現(xiàn)動(dòng)態(tài)添加新元素、刪除元素方法
這篇文章主要介紹了原生js實(shí)現(xiàn)動(dòng)態(tài)添加新元素、刪除元素方法 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05JavaScript來(lái)實(shí)現(xiàn)打開(kāi)鏈接頁(yè)面的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇JavaScript來(lái)實(shí)現(xiàn)打開(kāi)鏈接頁(yè)面的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06微信小程序的注冊(cè)頁(yè)面包含倒計(jì)時(shí)驗(yàn)證碼、獲取用戶信息
本文通過(guò)實(shí)例代碼給大家介紹了微信小程序的注冊(cè)頁(yè)面包含倒計(jì)時(shí)驗(yàn)證碼、獲取用戶信息的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-05-05Echarts直角坐標(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-11js實(shí)現(xiàn)前端分頁(yè)頁(yè)碼管理
本文主要介紹了js實(shí)現(xiàn)前端分頁(yè)頁(yè)碼管理的具體方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01