<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        JavaScript面向對象繼承原理與實現方法分析

        來源:懂視網 責編:小采 時間:2020-11-27 22:10:30
        文檔

        JavaScript面向對象繼承原理與實現方法分析

        JavaScript面向對象繼承原理與實現方法分析:本文實例講述了JavaScript面向對象繼承原理與實現方法。分享給大家供大家參考,具體如下: 1、構造函數、原型和實例的關系 構造函數有一個原型屬性prototype指向一個原型對象。 原型對象包含一個指向構造函數的指針constructor 。 實例包含一個指向原型對
        推薦度:
        導讀JavaScript面向對象繼承原理與實現方法分析:本文實例講述了JavaScript面向對象繼承原理與實現方法。分享給大家供大家參考,具體如下: 1、構造函數、原型和實例的關系 構造函數有一個原型屬性prototype指向一個原型對象。 原型對象包含一個指向構造函數的指針constructor 。 實例包含一個指向原型對

        本文實例講述了JavaScript面向對象繼承原理與實現方法。分享給大家供大家參考,具體如下:

        1、構造函數、原型和實例的關系

        構造函數有一個原型屬性prototype指向一個原型對象。

        原型對象包含一個指向構造函數的指針constructor

        實例包含一個指向原型對象的內部指針[[prototype]]

        2、通過原型鏈實現繼承

        基本思想:利用原型讓一個引用類型繼承另一個引用類型的屬性和方法,子類型可以訪問超類型的所有屬性和方法。原型鏈的構建是將一個類型的實例賦值給另一個構造函數的原型實現的。實現的本質是重寫原型對象,代之以一個新類型的實例。

        function Person(name) {
         this.name = name;
        }
        Person.prototype.sayHello = function() {
         alert("Hello, " + this.name);
        }
        var person = new Person("Alice");
        person.sayHello(); // Hello, Alice
        function Student() {
        }
        Student.prototype = new Person("Bruce");
        Student.prototype.id = 16;
        Student.prototype.showId = function() {
         alert(this.id);
        }
        var student = new Student();
        student.sayHello(); // Hello, Bruce
        student.showId(); // 16
        
        

        注意:不能用對象字面量創建原型方法,這樣會重寫原型鏈,導致繼承無效。

        function Person(name) {
         this.name = name;
        }
        Person.prototype.sayHello = function() {
         alert("Hello, " + this.name);
        }
        var person = new Person("Alice");
        person.sayHello(); // Hello, Alice
        function Student() {
        }
        Student.prototype = new Person("Bruce");
        Student.prototype.id = 16;
        Student.prototype = {
         showId: function() {
         alert(this.id);
         }
        };
        var student = new Student();
        student.sayHello(); // 報錯:student.sayHello is not a function
        student.showId(); // 16
        
        

        student指向Student的原型,Student的原型又指向Person的原型。

        student.sayHello()原型鏈搜索機制:

        1)搜索student實例中是否有sayHello()

        2)搜索Student.prototype是否有sayHello()

        3)搜索Person.prototype是否有sayHello()

        子類型有時候需要覆蓋超類型的某個方法,或者需要添加超類型中不存在的某個方法。

        function Person(name) {
         this.name = name;
        }
        Person.prototype.sayHello = function() {
         alert("Hello, " + this.name);
        }
        var person = new Person("Alice");
        person.sayHello(); // Hello, Alice
        function Student() {
        }
        Student.prototype = new Person("Bruce");
        Student.prototype.id = 16;
        Student.prototype.showId = function() {
        alert(this.id);
        }
        Student.prototype.sayHello = function() {
         alert("Hi, " + this.name);
        }
        var student = new Student();
        student.sayHello(); //Hi, Bruce
        student.showId(); // 16
        
        

        注意:給原型覆蓋或添加方法的代碼一定要放在替換原型的語句之后。

        function Person(name) {
         this.name = name;
        }
        Person.prototype.sayHello = function() {
         alert("Hello, " + this.name);
        }
        var person = new Person("Alice");
        person.sayHello(); // Hello, Alice
        function Student() {
        }
        Student.prototype.sayHello = function() {
         alert("Hi, " + this.name);
        }
        Student.prototype = new Person("Bruce");
        Student.prototype.id = 16;
        Student.prototype.showId = function() {
        alert(this.id);
        }
        var student = new Student();
        student.sayHello(); // Hello, Bruce
        student.showId(); // 16
        
        

        確定實例和原型的關系:

        (1)instanceof

        alert(student instanceof Object); // true
        alert(student instanceof Student); // true
        alert(student instanceof Person); // true
        
        

        (2)isProtptypeOf

        alert(Object.prototype.isPrototypeOf(student)); // true
        alert(Student.prototype.isPrototypeOf(student)); // true
        alert(Person.prototype.isPrototypeOf(student)); // true
        
        

        (3)getPrototypeOf

        Object.getPrototypeOf(student1) == Student.prototype
        
        

        使用原型鏈實現繼承的問題:

        (1)引用類型的屬性會被實例共享,原型實現繼承時,原型會變成另外一個類型的實例,實例的屬性則變成了現在的原型屬性,從而被共享。

        function Person(name, age) {
         this.friends = ["Cindy","David"];
        }
        function Student() {
        }
        Student.prototype = new Person();
        var student1 = new Student();
        student1.friends.push("Bruce");
        alert(student1.friends); // "Cindy","David","Bruce"
        var student2 = new Student();
        alert(student1.friends); // "Cindy","David","Bruce"
        
        

        (2)在創建子類型的實例時,不能向超類型的構造函數中傳遞參數,實際上,應該是沒有辦法在不影響所有對象實例的情況下,給超類型的構造函數傳遞參數。

        實際中很少單獨使用原型鏈實現繼承。

        3、通過構造函數實現繼承

        基本思想:在子類型構造函數的內部調用超類型構造函數。通過使用apply()call()方法也可以在新創建的對象上執行構造函數。

        function Person(name, age) {
         this.name = name;
         this.age = age;
        }
        function Student() {
         Person.call(this,"Alice",22); // 繼承了構造函數Person,同時還傳遞了參數
         this.id = 16; // 實例屬性
        }
        var student = new Student();
        alert(student.name); // "Alice"
        alert(student.age); // 22
        alert(student.id); // 16
        
        

        使用構造函數實現繼承的問題:

        (1)在超類型的原型中定義的方法,對子類型而言是不可見的,結果所有類型都只能使用構造函數模式。

        (2)要想子類能夠訪問超類定義的方法,方法只能在構造函數中定義,但方法在構造函數中定義時,函數復用無從談起。

        function Person(name, age) {
         this.name = name;
         this.age = age;
        }
        Person.prototype.showName = function() {
         alert(this.name);
        };
        function Student() {
         Person.call(this,"Alice",22);
         this.id = 16;
        }
        var student = new Student();
        alert(student.showName()); // 報錯:student.showName is not a function
        
        

        實際中很少單獨使用使用構造函數實現繼承。

        4、組合使用原型鏈和構造函數實現繼承

        思路:使用原型鏈繼承共享的屬性和方法,使用構造函數繼承實例屬性。

        效果:既通過在原型上定義方法實現了函數復用,又能夠保證每個實例都有自己的屬性。

        function Person(name, age) {
         this.name = name;
         this.age = age;
         this.friends = ["Cindy","David"];
        }
        Person.prototype.sayHello = function() {
         alert("Hello, " + this.name);
        }
        function Student(name, age, id) {
         Person.call(this, name, age);
         this.id = id;
        }
        Student.prototype = new Person();
        Student.prototype.showId = function() {
         alert(this.id);
        }
        var student1 = new Student("Alice", 22, 16);
        student1.friends.push("Emy");
        alert(student1.friends); // "Cindy","David","Emy"
        student1.sayHello(); // Hello, Alice
        student1.showId(); // 16
        var student2 = new Student("Bruce", 23, 17);
        alert(student2.friends); // "Cindy","David"
        student2.sayHello(); // Hello, Bruce
        student2.showId(); // 17
        
        

        更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《javascript面向對象入門教程》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》

        希望本文所述對大家JavaScript程序設計有所幫助。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        JavaScript面向對象繼承原理與實現方法分析

        JavaScript面向對象繼承原理與實現方法分析:本文實例講述了JavaScript面向對象繼承原理與實現方法。分享給大家供大家參考,具體如下: 1、構造函數、原型和實例的關系 構造函數有一個原型屬性prototype指向一個原型對象。 原型對象包含一個指向構造函數的指針constructor 。 實例包含一個指向原型對
        推薦度:
        標簽: 原理 js 方式
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产一区二区三区免费视频| 亚洲精品国产自在久久| 日韩一区二区a片免费观看| 久久精品亚洲综合一品| 九九精品成人免费国产片| 精品久久香蕉国产线看观看亚洲| 亚洲精品视频免费看| 最近免费中文字幕mv在线电影| 亚洲国产综合91精品麻豆| 91香蕉国产线在线观看免费| 亚洲欧洲春色校园另类小说| 国产精品入口麻豆免费观看| 最新亚洲卡一卡二卡三新区| 日韩中文无码有码免费视频| 特级av毛片免费观看| 国产亚洲一区二区在线观看| 日韩av无码久久精品免费| 亚洲精品WWW久久久久久| 久久国产一片免费观看| 久久亚洲免费视频| 国产精品69白浆在线观看免费| 人人狠狠综合久久亚洲88| 老司机69精品成免费视频| 亚洲人成在线精品| 国产一区二区三区在线免费观看 | 国产男女爽爽爽爽爽免费视频| 亚洲伊人精品综合在合线| 国产美女做a免费视频软件| 国产无限免费观看黄网站| 久久亚洲精品成人无码网站| 成人免费在线观看网站| 亚洲人成网站18禁止久久影院 | 伊人久久免费视频| 亚洲成a人片在线看| 亚洲高清视频一视频二视频三| 亚洲人成高清在线播放| 免费大黄网站在线观看| 国产成人亚洲毛片| 亚洲av无码一区二区乱子伦as| 久久精品女人天堂AV免费观看| 一级毛片**免费看试看20分钟|