當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
JavaScript中的的面向对象中的一些知识
生活随笔
收集整理的這篇文章主要介紹了
JavaScript中的的面向对象中的一些知识
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JavaScript中的的面向?qū)ο笾械囊恍┲R(shí)
function Cat(name,age){return {name:name,age:age }}//構(gòu)造函數(shù)function Dog(name,age){this.name=name;this.age=age; }function show(){var c1=new Cat("cat1",18);var c2=new Cat("cat2",19);//Javascript還提供了一個(gè)instanceof運(yùn)算符,驗(yàn)證原型對(duì)象與實(shí)例對(duì)象之間的關(guān)系。alert(c1 instanceof Cat); //falsealert(c2 instanceof Cat); //falsevar d1=new Dog("dog1",18);var d2=new Dog("dog2",19);alert(d1 instanceof Dog); //truealert(d2 instanceof Dog); //true }
構(gòu)造函數(shù)方法很好用,但是存在一個(gè)浪費(fèi)內(nèi)存的問(wèn)題。
所以就有了prototype對(duì)象了
function Dog(name,age){this.name=name;this.age=age; }Dog.prototype.type="Animal";//isPrototypeOf();某個(gè)prototype對(duì)象和某個(gè)實(shí)例之間的關(guān)系function show(){var dog=new Dog("yuanzhangliu",18);//alert(Dog.prototype.isPrototypeOf(dog));//true;//判斷對(duì)象的實(shí)例時(shí)屬于本地還是prototype對(duì)象的屬性alert(dog.hasOwnProperty("name")); //truealert(dog.hasOwnProperty("type")); //false//那么我們的in運(yùn)算符號(hào)可以判斷,某個(gè)實(shí)例是否含有屬性;不管本地還是prototype中滴alert("name" in dog); //truealert("type" in dog); //true//或者直接使用我們for循環(huán)來(lái)遍歷滴呀for(var prop in dog){alert(prop); }}第二部分:構(gòu)造函數(shù)之間的繼承
call apply 實(shí)現(xiàn)繼承
function Animal(){this.species="Animal"; this.fun=function (){alert("eat"); }}function Dog(name,age){this.name=name;this.age=age;Animal.call(this);//Animal.call(this,arguments); 將Dog中的參數(shù)傳遞過(guò)去,//讓它(arguments)中可以使用滴呀 具體的額,后面用到再呵呵呵 }function show(){var d=new Dog("dog",12);alert(d.species);d.fun();}prototype來(lái)是實(shí)現(xiàn)滴呀
function Animal(){this.species="Animal"; this.fun=function (){alert("eat"); }}function Dog(name,age){this.name=name;this.age=age;}function show(){//第一步:Dog.prototype=new Animal();Dog.prototype.constructor=Dog; //這不還是很重要的//防止繼承鏈的紊亂//再實(shí)際的開(kāi)發(fā)中如果我們替換了原來(lái)函數(shù)對(duì)象的prototype//我們也該constructor,(正確的說(shuō)叫還原constructor)//o.prototype={};//o.prototype.consturtor=ovar d=new Dog("yuanzhang.liu",18);d.fun();}方法三
function AnimalOld(){this.species="Animal"; this.fun=function (){alert("eat"); }}//將他改造成如下:function Animal(){}Animal.prototype.species="Animal";Animal.prototype.fun=function (){alert("eat"); }function Dog(name,age){this.name=name;this.age=age;}function show011(){//這樣我們就省去了new Animal();Dog.prototype=Animal.prototype;Dog.prototype.constructor=Dog; //這句話也將我們Animal.prototype.constructor 改變了滴呀//解決方法就是利用空的對(duì)象做為介質(zhì)滴呀var d=new Dog("yuanzhangliu",18);d.fun(); //這樣我們的額Dog.prototype 和 Animal.prototype 就指向了同一個(gè)//對(duì)象,任何一個(gè)對(duì)象的修改,都會(huì)反映到另外一個(gè)對(duì)象上滴呀 }function F(){}function show(){F.prototype=Animal.prototype;Dog.prototype=new F();Dog.prototype.constructor=Dog;var d=new Dog("yuanzhangliu",18);d.fun(); //這樣我們的額Dog.prototype 和 Animal.prototype 就指向了同一個(gè)//對(duì)象,任何一個(gè)對(duì)象的修改,都會(huì)反映到另外一個(gè)對(duì)象上滴呀}我們把上面的的方法總結(jié)和改進(jìn)一下就可以提取出一個(gè)通用的方法滴呀
function extend(Child,Parent){function Kong(){};Kong.prototype=Parent.prototype;Kong.prototype.constructor=Kong;Child.prototype=new Kong();Child.prototype.constructor=Child;}?
方式四:我們可以通過(guò)拷貝的方式進(jìn)行滴呀;
function Person(name,age){this.name=name;this.age=age;this.fun=function (){alert("fun"); }}Person.prototype.protoFun=function (){alert("prototypeInfo"); }function Dog(name,age){this.name=name;this.age=age;}//接下來(lái),我們通過(guò)拷貝來(lái)實(shí)現(xiàn)繼承id呀function extend2(Child,Parent){//同樣的,使用這種方式實(shí)現(xiàn)繼承滴呀;//父類的屬性,必須設(shè)置在prototype上才被子類繼承var p=Parent.prototype;var c=Child.prototype;for(var i in p){c[i]=p[i];//還要拷貝它的值滴呀; }}function show(){extend2(Dog,Animal);var d=new Dog("yuanzhangliu",18);d.protoFun();d.fun();//無(wú)法調(diào)用滴呀 }非構(gòu)造函數(shù)的方式來(lái)實(shí)現(xiàn)繼承繼承滴呀
//非構(gòu)造函數(shù)的方式實(shí)現(xiàn)繼承滴呀;var chinese={nation:'中國(guó)'};var Doctor={career:'醫(yī)生'}//這兩個(gè)都是普通的對(duì)象,不是構(gòu)造函數(shù),無(wú)法使用構(gòu)造函數(shù)的方式來(lái)實(shí)現(xiàn)繼承滴呀//將兩個(gè)對(duì)象通過(guò)prototype鏈,連接在一在一起的呀;實(shí)現(xiàn)方式
function object(parent){function F(){//利用它的原型鏈,將他們連接在一起的滴呀 }F.prototype=parent;var son=new F();return son;//這樣就返回了父級(jí)對(duì)象滴呀 }//使用的時(shí)候;var Chinese={nation:'中國(guó)' }var Doctor=object(Chinese);Doctor.career='醫(yī)生';alert(Doctor.nation);//原來(lái)尼瑪,是這樣寫(xiě)的滴呀;還用一種方式:
function extendCopy(parent){var c={}; //空對(duì)象介質(zhì);for(var i in parent){//循環(huán)遍歷出它的屬性的呀; 子對(duì)象獲取的是父對(duì)象的一個(gè)內(nèi)存地址c[i]=parent[i]; }return c;}//使用的時(shí)候;var Chinese={nation:'中國(guó)' }var Doctor=extendCopy(Chinese);Doctor.career='醫(yī)生';alert(Doctor.nation);缺點(diǎn):淺拷貝,子對(duì)象獲取的是父對(duì)象的一個(gè)內(nèi)存地址,當(dāng)子對(duì)象改變的同時(shí)也就改變了父級(jí)對(duì)象滴呀
Chinese.birthPlaces=['日本','京都','大阪'];var Doctor=extendCopy(Chinese);Doctor.birthPlaces.push('眉山');alert(Chinese.birthPlaces);//父級(jí)的也被改變了 alert(Doctor.birthPlaces);//他們的結(jié)果都是:'日本','京都','大阪','眉山'?
既然有淺拷貝,當(dāng)然就有我們的深拷滴呀
function deepCopy(parent,son){var son=son || {};for(var i in parent){if(typeof parent[i]==='object'){son[i]=(parent[i].constructor===Array)?[]:{};//然后繼續(xù)拷貝滴呀deepCopy(parent[i],son[i]);//進(jìn)一步的遍歷滴呀}else{son[i]=parent[i]; }}}//目前jquery 就使用的這種方式實(shí)現(xiàn)繼承id呀這樣我們的面向?qū)ο蟮木退闶?...
?
轉(zhuǎn)載于:https://www.cnblogs.com/mc67/p/5182247.html
總結(jié)
以上是生活随笔為你收集整理的JavaScript中的的面向对象中的一些知识的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Sublime Text添加插入带当前时
- 下一篇: JS 计算日期天数差