<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關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題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關(guān)鍵字專題關(guān)鍵字專題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
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        老生常談javascript的面向?qū)ο笏枷?/h1>
        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:31:55
        文檔

        老生常談javascript的面向?qū)ο笏枷?/h4>
        老生常談javascript的面向?qū)ο笏枷?面向?qū)ο蟮娜蠡咎匦?封裝(把相關(guān)的信息(無論數(shù)據(jù)或方法)存儲在對象中的能力) 繼承(由另一個類(或多個類)得來類的屬性和方法的能力) 多態(tài)(一個對象在不同情況下的多種形態(tài)) 定義類或?qū)ο?第一種:基于Object對象 var person = new Obje
        推薦度:

        導(dǎo)讀老生常談javascript的面向?qū)ο笏枷?面向?qū)ο蟮娜蠡咎匦?封裝(把相關(guān)的信息(無論數(shù)據(jù)或方法)存儲在對象中的能力) 繼承(由另一個類(或多個類)得來類的屬性和方法的能力) 多態(tài)(一個對象在不同情況下的多種形態(tài)) 定義類或?qū)ο?第一種:基于Object對象 var person = new Obje

        面向?qū)ο蟮娜蠡咎匦?/p>

        封裝(把相關(guān)的信息(無論數(shù)據(jù)或方法)存儲在對象中的能力)

        繼承(由另一個類(或多個類)得來類的屬性和方法的能力)

        多態(tài)(一個對象在不同情況下的多種形態(tài))

        定義類或?qū)ο?/p>

        第一種:基于Object對象

        var person = new Object();
        person.name = "Rose";
        person.age = 18;
        person.getName = function () {
         return this.name;
        };
        console.log(person.name);//Rose
        console.log(person.getName);//function () {return this.name;}
        console.log(person.getName());//Rose
         

        缺點:不能創(chuàng)建多個對象。

        第二種:基于字面量方式

        var person = {
         name : "Rose",
         age : 18 ,
         getName : function () {
         return this.name;
         }
        };
        console.log(person.name);//Rose
        console.log(person.getName);//function () {return this.name;}
        console.log(person.getName());//Rose
        

        優(yōu)點:比較清楚的查找對象包含的屬性和方法;

        缺點:不能創(chuàng)建多個對象。

        第三種:工廠模式

        方式一:

        function createPerson(name,age) {
         var object = new Object();
         object.name = name;
         object.age = age;
         object.getName = function () {
         return this.name;
         };
         return object;
        }
        var person1 = createPerson("Rose",18);
        var person2 = createPerson("Jack",20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName);//false//重復(fù)生成函數(shù),為每個對象都創(chuàng)建獨立的函數(shù)版本
         
        
        

        優(yōu)點:可以創(chuàng)建多個對象;

        缺點:重復(fù)生成函數(shù)getName(),為每個對象都創(chuàng)建獨立的函數(shù)版本。

        方式二:

        function createPerson(name,age) {
         var object = new Object();
         object.name = name;
         object.age = age;
         object.getName = getName;
         return object;
        }
        function getName() {
         return this.name;
        }
        var person1 = createPerson("Rose",18);
        var person2 = createPerson("Jack",20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName);//true//共享同一個函數(shù)
        

        優(yōu)點:可以創(chuàng)建多個對象;

        缺點:從語義上講,函數(shù)getName()不太像是Person對象的方法,辨識度不高。

        第四種:構(gòu)造函數(shù)方式

        方式一:

        function Person(name,age) {
         this.name = name;
         this.age = age;
         this.getName = function () {
         return this.name;
         }
        }
        var person1 = new Person("Rose",18);
        var person2 = new Person("Jack",20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName); //false//重復(fù)生成函數(shù),為每個對象都創(chuàng)建獨立的函數(shù)版本
        

        優(yōu)點:可以創(chuàng)建多個對象;

        缺點:重復(fù)生成函數(shù)getName(),為每個對象都創(chuàng)建獨立的函數(shù)版本。

        方式二:

        function Person(name,age) {
         this.name = name;
         this.age = age;
         this.getName = getName ;
        }
        function getName() {
         return this.name;
        }
        var person1 = new Person("Rose",18);
        var person2 = new Person("Jack",20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName); //true//共享同一個函數(shù)
         
        

        優(yōu)點:可以創(chuàng)建多個對象;

        缺點:從語義上講,函數(shù)getName()不太像是Person對象的方法,辨識度不高。

        第五種:原型方式

        function Person() {
        }
        Person.prototype.name = 'Rose';
        Person.prototype.age = 18;
        Person.prototype.getName = function () {
         return this.name;
        };
        var person1 = new Person();
        var person2 = new Person();
        console.log(person1.name);//Rose
        console.log(person2.name);//Rose//共享同一個屬性
        console.log(person1.getName === person2.getName);//true//共享同一個函數(shù)
        

        缺點:它省略了為構(gòu)造函數(shù)傳遞初始化參數(shù),這在一定程序帶來不便;另外,最主要是當(dāng)對象的屬性是引用類型時,它的值是不變的,總是引用同一個外部對象,所有實例對該對象的操作都會影響其它實例:

        function Person() {
        }
        Person.prototype.name = 'Rose';
        Person.prototype.age = 18;
        Person.prototype.lessons = ["語文","數(shù)學(xué)"];
        Person.prototype.getName = function () {
         return this.name;
        };
        var person1 = new Person();
        person1.lessons.push("英語");
        var person2 = new Person();
        console.log(person1.lessons);//["語文", "數(shù)學(xué)", "英語"]
        console.log(person2.lessons);//["語文", "數(shù)學(xué)", "英語"]//person1修改影響了person2
        

        第六種:構(gòu)造函數(shù)+原型方式(推薦)

        function Person(name,age) {
         this.name = name;
         this.age = age;
        }
        Person.prototype.getName = function () {
         return this.name;
        };
        var person1 = new Person('Rose', 18);
        var person2 = new Person('Jack', 20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName);//true//共享原型中定義的方法
        

        缺點:屬性定義在構(gòu)造函數(shù)內(nèi),方法定義在構(gòu)造函數(shù)外,與面向?qū)ο蟮姆庋b思想不符。

        第七種:構(gòu)造函數(shù)+動態(tài)原型方式(推薦)

        方式一:

        function Person(name,age) {
         this.name = name;
         this.age = age;
         if (typeof Person._getName === "undefined"){
         Person.prototype.getName = function () {
         return this.name;
         };
         Person._getName = true;
         }
        }
        var person1 = new Person('Rose', 18);
        var person2 = new Person('Jack', 20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName);//true//共享原型中定義的方法
        

        方式二:

        function Person(name,age) {
         this.name = name;
         this.age = age;
         if (typeof this.getName !== "function"){
         Person.prototype.getName = function () {
         return this.name;
         };
         }
        }
        var person1 = new Person('Rose', 18);
        var person2 = new Person('Jack', 20);
        console.log(person1.name);//Rose
        console.log(person2.name);//Jack
        console.log(person1.getName === person2.getName);//true//共享原型中定義的方法
        

        對象屬性的擴展及刪除

        Javascript的對象可以使用 '.' 操作符動態(tài)的擴展其屬性,可以使用 'delete' 關(guān)鍵字或?qū)傩缘闹翟O(shè)置為 'undefined' 來刪除屬性。

        function Person(name,age) {
         this.name = name;
         this.age = age;
         if (typeof Person._getName === "undefined"){
         Person.prototype.getName = function () {
         return this.name;
         };
         Person._getName = true;
         }
        }
        var person = new Person("Rose",18);
        person.job = 'Engineer';//添加屬性
        console.log(person.job);//Engineer
        delete person.job;//刪除屬性
        console.log(person.job);//undefined//刪除屬性后值為undefined
        person.age = undefined;//刪除屬性
        console.log(person.age);//undefined//刪除屬性后值為undefined
        

        對象屬性類型

        數(shù)據(jù)屬性

        特性:

        [configurable]:表示能否使用delete操作符刪除從而重新定義,或能否修改為訪問器屬性。默認為true;

        [enumberable]:表示是否可通過for-in循環(huán)返回屬性。默認true;

        [writable]:表示是否可修改屬性的值。默認true;

        [value]:包含該屬性的數(shù)據(jù)值。讀取/寫入都是該值。默認為undefined;如上面實例對象person中定義了name屬性,其值為'My name',對該值的修改都反正在這個位置

        function Person(name,age) {
         this.name = name;
         this.age = age;
         if (typeof Person._getName === "undefined"){
         Person.prototype.getName = function () {
         return this.name;
         };
         Person._getName = true;
         }
        }
        var person = new Person("Rose",18);
        Object.defineProperty(person,"name",{configurable:false,writable:false});
        person.name = "Jack";
        console.log(person.name);//Rose//重新賦值無效
        delete person.name;
        console.log(person.name);//Rose//刪除無效
        

        注意:

        一旦將configurable設(shè)置為false,則無法再使用defineProperty將其修改為true(執(zhí)行會報錯:cannot redefine property : propertyName)

        function Person(name,age) {
         this.name = name;
         this.age = age;
         if (typeof Person._getName === "undefined"){
         Person.prototype.getName = function () {
         return this.name;
         };
         Person._getName = true;
         }
        }
        var person = new Person("Rose",18);
        Object.defineProperty(person,"name",{configurable:false,writable:false});
        person.name = "Jack";
        console.log(person.name);//Rose//重新賦值無效
        delete person.name;
        console.log(person.name);//Rose//刪除無效
        Object.defineProperty(person,"name",{configurable:true,writable:true});//Cannot redefine property: name
        

        訪問器屬性

        特性:

        [configurable]:是否可通過delete操作符刪除重新定義屬性;

        [numberable]:是否可通過for-in循環(huán)查找該屬性;

        [get]:讀取屬性時調(diào)用,默認:undefined;

        [set]:寫入屬性時調(diào)用,默認:undefined;

        訪問器屬性不能直接定義,必須使用defineProperty()或defineProperties來定義:如下

        function Person(name,age) {
         this.name = name;
         this._age = age;
         if (typeof Person._getName === "undefined"){
         Person.prototype.getName = function () {
         return this.name;
         };
         Person._getName = true;
         }
        }
        var person = new Person("Rose",18);
        Object.defineProperty(person,"age",{
         get:function () {
         return this._age;
         },
         set:function (age) {
         this._age = age;
         }});
        person.age = 20;
        console.log(person.age);//20//person.age=20是使用set方法將20賦值給_age,person.age是使用get方法將_age的讀取出來
        console.log(person._age);//20
        

        獲取所有的屬性和屬性的特性

        使用Object.getOwnPropertyNames(object)方法可以獲取所有的屬性;

        使用Object.getOwnPropertyDescriptor(object,property)方法可以取得給定屬性的特性;

        function Person(name,age) {
         this.name = name;
         this._age = age;
         if (typeof Person._getName === "undefined"){
         Person.prototype.getName = function () {
         return this.name;
         };
         Person._getName = true;
         }
        }
        var person = new Person("Rose",18);
        Object.defineProperty(person,"age",{
         get:function () {
         return this._age;
         },
         set:function (age) {
         this._age = age;
         }});
        console.log(Object.getOwnPropertyNames(person));//["name", "_age", "age"]
        console.log(Object.getOwnPropertyDescriptor(person,"age"));//{enumerable: false, configurable: false, get: function, set: function}
        

        對于數(shù)據(jù)屬性,可以取得:configurable,enumberable,writable和value;

        對于訪問器屬性,可以取得:configurable,enumberable,get和set;

        繼承機制實現(xiàn)

        對象冒充

        function Father(name) {
         this.name = name ;
         this.getName = function () {
         return this.name;
         }
        }
        function Son(name,age) {
         this._newMethod = Father;
         this._newMethod(name);
         delete this._newMethod;
        
         this.age = age;
         this.getAge = function () {
         return this.age;
         }
        }
        var father = new Father("Tom");
        var son = new Son("Jack",18);
        console.log(father.getName());//Tom
        console.log(son.getName());//Jack//繼承父類getName()方法
        console.log(son.getAge());//18
        
        

        多繼承(利用對象冒充可以實現(xiàn)多繼承)

        function FatherA(name) {
         this.name = name ;
         this.getName = function () {
         return this.name;
         }
        }
        function FatherB(job) {
         this.job = job;
         this.getJob = function () {
         return this.job;
         }
        }
        function Son(name,job,age) {
         this._newMethod = FatherA;
         this._newMethod(name);
         delete this._newMethod;
         this._newMethod = FatherB;
         this._newMethod(job);
         delete this._newMethod;
        
         this.age = age;
         this.getAge = function () {
         return this.age;
         }
        }
        var fatherA = new FatherA("Tom");
        var fatherB = new FatherB("Engineer");
        var son = new Son("Jack","Programmer",18);
        console.log(fatherA.getName());//Tom
        console.log(fatherB.getJob());//Engineer
        console.log(son.getName());//Jack//繼承父類FatherA的getName()方法
        console.log(son.getJob());//Programmer//繼承父類FatherB的getJob()方法
        console.log(son.getAge());//18
         
        

        call()方法

        function Father(name) {
         this.name = name ;
         this.getName = function () {
         return this.name;
         }
        }
        function Son(name,job,age) {
         Father.call(this,name);
        
         this.age = age;
         this.getAge = function () {
         return this.age;
         }
        }
        var father = new Father("Tom");
        var son = new Son("Jack","Programmer",18);
        console.log(father.getName());//Tom
        console.log(son.getName());//Jack//繼承父類getName()方法
        console.log(son.getAge());//18
        
        

        多繼承(利用call()方法實現(xiàn)多繼承)

        function FatherA(name) {
         this.name = name ;
         this.getName = function () {
         return this.name;
         }
        }
        function FatherB(job) {
         this.job = job;
         this.getJob = function () {
         return this.job;
         }
        }
        function Son(name,job,age) {
         FatherA.call(this,name);
         FatherB.call(this,job);
        
         this.age = age;
         this.getAge = function () {
         return this.age;
         }
        }
        var fatherA = new FatherA("Tom");
        var fatherB = new FatherB("Engineer");
        var son = new Son("Jack","Programmer",18);
        console.log(fatherA.getName());//Tom
        console.log(fatherB.getJob());//Engineer
        console.log(son.getName());//Jack//繼承父類FatherA的getName()方法
        console.log(son.getJob());//Programmer//繼承父類FatherB的getJob()方法
        console.log(son.getAge());//18
        
        

        apply()方法

        function Father(name) {
         this.name = name ;
         this.getName = function () {
         return this.name;
         }
        }
        function Son(name,job,age) {
         Father.apply(this,new Array(name));
        
         this.age = age;
         this.getAge = function () {
         return this.age;
         }
        }
        var father = new Father("Tom");
        var son = new Son("Jack","Programmer",18);
        console.log(father.getName());//Tom
        console.log(son.getName());//Jack//繼承父類getName()方法
        console.log(son.getAge());//18
        
        

        多繼承(利用apply()方法實現(xiàn)多繼承)

        function FatherA(name) {
         this.name = name ;
         this.getName = function () {
         return this.name;
         }
        }
        function FatherB(job) {
         this.job = job;
         this.getJob = function () {
         return this.job;
         }
        }
        function Son(name,job,age) {
         FatherA.apply(this,new Array(name));
         FatherB.apply(this,new Array(job));
        
         this.age = age;
         this.getAge = function () {
         return this.age;
         }
        }
        var fatherA = new FatherA("Tom");
        var fatherB = new FatherB("Engineer");
        var son = new Son("Jack","Programmer",18);
        console.log(fatherA.getName());//Tom
        console.log(fatherB.getJob());//Engineer
        console.log(son.getName());//Jack//繼承父類FatherA的getName()方法
        console.log(son.getJob());//Programmer//繼承父類FatherB的getJob()方法
        console.log(son.getAge());//18
        
        

        原型鏈方法

        function Father() {
        }
        Father.prototype.name = "Tom";
        Father.prototype.getName = function () {
         return this.name;
        };
        function Son() {
        }
        Son.prototype = new Father();
        Son.prototype.age = 18;
        Son.prototype.getAge = function () {
         return this.age;
        };
        var father = new Father();
        var son = new Son();
        console.log(father.getName());//Tom
        console.log(son.getName());//Tom//繼承父類FatherA的getName()方法
        console.log(son.getAge());//18
         
        

        混合方式(call()+原型鏈)

        function Father(name) {
         this.name = name;
        }
        Father.prototype.getName = function () {
         return this.name;
        };
        function Son(name,age) {
         Father.call(this,name);
         this.age = age;
        }
        Son.prototype = new Father();
        Son.prototype.getAge = function () {
         return this.age;
        };
        var father = new Father("Tom");
        var son = new Son("Jack",18);
        console.log(father.getName());//Tom
        console.log(son.getName());//Jack//繼承父類Father的getName()方法
        console.log(son.getAge());//18
        

        多態(tài)機制實現(xiàn)

        function Person(name) {
         this.name = name;
         if (typeof this.getName !== "function"){
         Person.prototype.getName = function () {
         return this.name;
         }
         }
         if (typeof this.toEat !== "function"){
         Person.prototype.toEat = function (animal) {
         console.log( this.getName() + "說去吃飯:");
         animal.eat();
         }
         }
        }
        function Animal(name) {
         this.name = name;
         if (typeof this.getName !== "function"){
         Animal.prototype.getName = function () {
         return this.name;
         }
         }
        }
        function Cat(name) {
         Animal.call(this,name);
         if (typeof this.eat !== "function"){
         Cat.prototype.eat = function () {
         console.log(this.getName() + "吃魚");
         }
         }
        }
        Cat.prototype = new Animal();
        function Dog(name) {
         Animal.call(this,name);
         if (typeof this.eat !== "function"){
         Dog.prototype.eat = function () {
         console.log(this.getName() + "啃骨頭");
         }
         }
        }
        Dog.prototype = new Animal();
        
        var person = new Person("Tom");
        person.toEat(new Cat("cat"));//Tom說去吃飯:cat吃魚
        person.toEat(new Dog("dog"));//Tom說去吃飯:dog啃骨頭
        
        

        以上這篇老生常談javascript的面向?qū)ο笏枷刖褪切【幏窒斫o大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

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

        文檔

        老生常談javascript的面向?qū)ο笏枷?/h4>
        老生常談javascript的面向?qū)ο笏枷?面向?qū)ο蟮娜蠡咎匦?封裝(把相關(guān)的信息(無論數(shù)據(jù)或方法)存儲在對象中的能力) 繼承(由另一個類(或多個類)得來類的屬性和方法的能力) 多態(tài)(一個對象在不同情況下的多種形態(tài)) 定義類或?qū)ο?第一種:基于Object對象 var person = new Obje
        推薦度:

        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲综合视频在线观看| 亚洲精品tv久久久久| 久久精品国产亚洲精品2020| 玖玖在线免费视频| 一本色道久久综合亚洲精品高清| 羞羞视频免费观看| 国产l精品国产亚洲区在线观看| 九九全国免费视频| 亚洲中文字幕无码久久综合网| 一级成人生活片免费看| 在线精品亚洲一区二区三区| 中文精品人人永久免费| 亚洲av之男人的天堂网站| 久久国产精品成人免费| 亚洲欧洲在线观看| 国产91色综合久久免费分享| 亚洲狠狠成人综合网| 国产zzjjzzjj视频全免费| 产传媒61国产免费| 亚洲av午夜福利精品一区| 亚洲免费视频播放| 亚洲av无一区二区三区| 久久久久亚洲av成人无码电影 | 亚洲综合AV在线在线播放| 皇色在线免费视频| 亚洲91av视频| 成人黄软件网18免费下载成人黄18免费视频| 亚洲国产精品无码第一区二区三区| 国产色婷婷精品免费视频| 三上悠亚在线观看免费| 18亚洲男同志videos网站| 日本特黄特色aa大片免费| 美女无遮挡拍拍拍免费视频 | 午夜免费福利在线| rh男男车车的车车免费网站| 亚洲国产高清人在线| 日韩亚洲国产高清免费视频| 一级毛片视频免费观看 | 亚洲日本va一区二区三区 | 亚洲另类少妇17p| 18禁黄网站禁片免费观看不卡|