1. Put the Curd method of song management into the prototype
2, in the constructor, we have only one property is Songlist, because the music library is not common, if the songlist into the prototype, any one of the changes songlist, will songlist change, The next man, New Songlist, will be different.
3, the Crud method into the prototype, a lot of benefits, to avoid wasting name pollution, avoid unnecessary memory waste
4, note the method of the current object, the other methods of invoking the current object need to use this, the this refers to the current object
<Script> functionSongmanager () { This. SongList= NULL; } //In the method of the current object, call another method of the current object, using this //For example, call Selectsong in the Removesong method This.selectsongSongmanager.prototype={init:function(songList) { This. SongList=songList; }, Addsong:function(song) { This. Songlist.push (song); }, Removesong:function(songname) {varSong= This. Selectsong (Songname); if(Song== NULL){ Throw "the song you want to delete does not exist! Please try again"; } varIndex= This. Songlist.indexof (song); This. Songlist.splice (Index,1); }, Updatesong:function(Songname, singer) {varSong= This. Selectsong (Songname); if(Song== NULL){ Throw "the song you want to edit does not exist! Please try again"; } Song.singer=singer; }, Selectsong:function(songname) { for (vark= 0; k< This. songlist.length; k++) { varSong= This. Songlist[k]; if(Song.songname==songname) { returnSong; } } return NULL; } }; varPwbdemanager= NewSongmanager (); Pwbdemanager.init ([{songname:"Qinghai-Tibet Plateau", singer:"Pan Wenbin"}, {songname:"my sneakers, friction and friction are the most fashionable", singer:"Johnson, Pommeron" } ]); Pwbdemanager.addsong ({songname:"Dongfeng Broken", singer:"Jay Chou" }) varGjbdemanager= NewSongmanager (); Gjbdemanager.init ([{songname:"Two Tigers", singer:"Gao Jinbiao"}, {songname:"Stucco maker", singer:"Gao Jinbiao" } ]); //gjbdemanager.removesong ("Li Bai");Gjbdemanager.removesong ("Two Tigers"); Console.log (pwbdemanager.songlist); Console.log (gjbdemanager.songlist);</Script>
JS Advanced--Song management