繼承

1、冒充方式-------實(shí)現(xiàn)繼承

<script type="text/javascript">

                    function CreatPerson (name,age) {
                            this.name = name;
                            this.age = age;
                            this.sayHi = function () {
                                    alert("哈哈");
                            }
                    }

                    function CreatStudent (name, age, banJi) {

// 通過(guò)冒充實(shí)現(xiàn)子類繼承父類的方法、屬性

                          this.newFn = CreatPerson; 

//1、 給this(也就是學(xué)生對(duì)象)添加一個(gè)新的方法,也就是CreatPerson這個(gè)構(gòu)造函數(shù)

                 this.newFn(name, age); 

//2、 執(zhí)行新添加進(jìn)去的函數(shù),通過(guò)this.newFn調(diào)用父類函數(shù),進(jìn)而修改了父類函數(shù)中this指針的指向,

                            delete this.newFn; // 3、刪除這個(gè)函數(shù)(過(guò)河拆橋)
                            
                            this.banJi = banJi;
                            
                            this.study = function () {
                                    alert("學(xué)習(xí)");
                            };
                            
                    }
                    
                    var stu1 = new CreatStudent("李威", 23, 9);
                    console.log(stu1.study);
            </script>

復(fù)制代碼

2、原型方式實(shí)現(xiàn)繼承

<script type="text/javascript">
                        function CreatAnimal (name, age) {
                                this.name = name;
                                this.age = age;
                                this.sayHi = function () {
                                        alert("你好");
                                };
                        }
                        CreatAnimal.prototype.gender = "男";
                        CreatAnimal.prototype.sayBye = function () {
                                alert("走吧");
                        } 
                        function CreatDog (name, age, leg) {
                                this.leg = leg;
                                this.lookDoor = function () {
                                        alert("看門");
                                }
                        }

把父類的對(duì)象當(dāng)做子類的原型,這樣就把子類的原型和父類的原型聯(lián)系在一起了

                        CreatDog.prototype = new CreatAnimal("李淑芬",12);

因?yàn)閷?duì)象的constructor屬性值是取決于原型中的constructor值的,而此時(shí)原型中constructor值指向的是父類函數(shù),所以要修改原型的constructor值為子類函數(shù),保證繼承關(guān)系不混亂

                    CreatDog.prototype.constructor = CreatDog;
                        
                        var dog = new CreatDog("旺財(cái)", 12, 4);
//                      console.log(dog.name);
                        console.log(dog.gender);
                        dog.sayBye();
                        console.log(dog.constructor == CreatDog);
                        
//                        dog.sayHi();
                        
                        
                        
                        
                </script>

3、call方式實(shí)現(xiàn)繼承

<script type="text/javascript">

//父類構(gòu)造函數(shù)
                function CreatPerson(name,age,gender){
                        this.name = name;
                        this.age = age;
                        this.gender = gender;
                        this.walk = function(){
                                console.log("走的舒服嗎")
                }
 //父類原型
                /*CreatPerson.prototype.walk = function(){
                        console.log("走的舒服嗎")*/
                }
                
//子類
                function CreatStudent(name,age,gender,hobby){
                        CreatPerson.call(this,name,age,gender)
                        this.hobby = hobby;
                        /*this.walk =function(){
                                console.log("走的爽不")
                        };*/
                        this.study = function(){
                                console.log("學(xué)的爽不")
                        }
                }
   //子類原型方法
                /*CreatStudent.prototype.walk = function(){
                        console.log("走的很不爽")
                }
                CreatStudent.prototype.study = function(){
                        console.log("學(xué)習(xí)好舒服")
                }*/
                
                
  //創(chuàng)建子對(duì)象
                var s1 = new CreatStudent("李培舟",23,"男","吃")
                console.log(s1.name);
                console.log(s1.age);
                console.log(s1.gender);
                console.log(s1.walk());
                console.log(s1.study());
                
                
        </script>

4、組合方式實(shí)現(xiàn)繼承

<script type="text/javascript">
        //父類構(gòu)造函數(shù)
                function CreatPerson(name,age,gender){
                        this.name = name;
                        this.age = age;
                        this.gender = gender;
                        /*this.walk = function(){
                                console.log("走的舒服嗎")*/
                }
        //父類原型
                CreatPerson.prototype.walk = function(){
                        console.log("走的舒服嗎")
                }
                
                //子類
                function CreatStudent(name,age,gender,hobby){
                        CreatPerson.call(this,name,age,gender)
                        this.hobby = hobby;
                        /*this.walk =function(){
                                console.log("走的爽不")
                        };*/
                        /*this.study = function(){
                                console.log("學(xué)的爽不")
                        }*/
                }
                //子類原型方法
                /*CreatStudent.prototype.walk = function(){
                        console.log("走的很不爽")
                }*/
                CreatStudent.prototype.study = function(){
                        console.log("學(xué)習(xí)好舒服")
                }
                
                CreatStudent.prototype = new CreatPerson();
                CreatStudent.prototype.constructor = CreatPerson;
                
                
                //創(chuàng)建子對(duì)象
                var s1 = new CreatStudent("李培舟",23,"男","吃")
                console.log(s1.name);
                console.log(s1.age);
                console.log(s1.gender);
                console.log(s1.hobby);
                console.log(s1.walk());
                </script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 繼承是面向?qū)ο笾幸粋€(gè)比較核心的概念。其他正統(tǒng)面向?qū)ο笳Z(yǔ)言都會(huì)用兩種方式實(shí)現(xiàn)繼承:一個(gè)是接口實(shí)現(xiàn),一個(gè)是繼承。而EC...
    dxxwdong閱讀 411評(píng)論 0 2
  • 博客內(nèi)容:什么是面向?qū)ο鬄槭裁匆嫦驅(qū)ο竺嫦驅(qū)ο缶幊痰奶匦院驮瓌t理解對(duì)象屬性創(chuàng)建對(duì)象繼承 什么是面向?qū)ο?面向?qū)ο?..
    _Dot912閱讀 1,527評(píng)論 3 12
  • 繼承 Javascript中繼承都基于兩種方式:1.通過(guò)原型鏈繼承,通過(guò)修改子類原型的指向,使得子類實(shí)例通過(guò)原型鏈...
    LeoCong閱讀 402評(píng)論 0 0
  • 1、構(gòu)造函數(shù)模式 [url=]file:///C:/Users/i037145/AppData/Local/Tem...
    橫沖直撞666閱讀 919評(píng)論 0 0
  • 本文先對(duì)es6發(fā)布之前javascript各種繼承實(shí)現(xiàn)方式進(jìn)行深入的分析比較,然后再介紹es6中對(duì)類繼承的支持以及...
    lazydu閱讀 16,822評(píng)論 7 44

友情鏈接更多精彩內(nèi)容