JavaScript面向对象之——继承【原】

  本列子参照JavaScript核心技术一书略作修改,JS中继承还远不止于此,本博只略作讲解

JS-Code//<![CDATA
        //基类
        function tune(title,type,test)
        {
            this.title = title;
            this.type = type;
            this.getTitle = function(){
                return "Song: " + this.title +" Type:" + this.type;
            }
        }
        //派生类
        function artist_tune(title,type,artist,test)
        {
            this.artist = artist;
            this.toString("Artist is"+artist);
            tune.call(this,title,artist);//call继承需列出参数
//            tune.apply(this,arguments);//apply直接使用参数列表,不包含最后一个参数,也就是说title,type,artist,test只取前三个参数
            this.toString = function(){
                return "Artist :"+this.artist+" "+this.getTitle();//调用继承基类的方法
            }
        }
       
//        artist_tune.prototype = new tune();
        var song = new artist_tune("I want to hold your hand ","rock","Beatles","test");
        alert(song.toString());
         //]>

你可能感兴趣的:(JavaScript)