JS Advanced--Song management

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.