日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
代碼詳解:使用JavaScript進行面向?qū)ο缶幊痰闹改?/div>

 一切都從對象開始。

10年積累的網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有華容免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

對象,即我們相互交流的一個載體,有其屬性和方法。對象是面向?qū)ο缶幊痰暮诵?,不僅用于JavaScript,而且還適用于Java、C語言、C++等。不再考慮單個變量和函數(shù),而選擇自給型的對象。

以下是在討論面向?qū)ο缶幊?OOP)時最常用到的概念:

  • 對象,屬性,方法
  • 封裝
  • 抽象
  • 復(fù)用/繼承
  • 多態(tài)性
  • 關(guān)聯(lián)
  • 聚合
  • 組合

1. 對象,屬性,方法

1.1 對象字面量(Objectliteral)

在大括號中設(shè)置屬性,從而在JavaScript中創(chuàng)建一個新對象。對象字面量屬性值可以是任何數(shù)據(jù)類型,如函數(shù)字面量、數(shù)組、字符串、數(shù)字或布爾值。

下面創(chuàng)建一個命名圖書的對象,其屬性包括作者、出版年份、標題和方法。

 
 
 
 
  1. — summary. 
  2.  
  3. constbook = { 
  4. title: "Hippie", 
  5. author: "Paulo Coelho", 
  6. year: "2018" 

對象創(chuàng)建完成后,可以使用點記法獲取值。例如,可以使用book.title.獲取標題的值,還可以使用方括號book[‘title’]訪問屬性。

1.2 對象構(gòu)造函數(shù)(Objectconstructor)

對象構(gòu)造函數(shù)與常規(guī)函數(shù)相同。每次創(chuàng)建對象時都會用到??蓪⑵渑c新關(guān)鍵字一起使用。當需要創(chuàng)建具有相同屬性和方法的多個對象時,對象構(gòu)造函數(shù)非常有用。

 
 
 
 
  1. constbook = { 
  2.  
  3. title: "Hippie", 
  4.  
  5. author: "Paulo Coelho", 
  6.  
  7. year: "2018" 
  8.  
  9. }const book1 = { 
  10.  
  11. title: "The Alchemist", 
  12.  
  13. author: "Paulo Coelho", 
  14.  
  15. year: "1988", 
  16.  

如果要創(chuàng)建多個書籍(book)對象,必須為每本書復(fù)制代碼。可以繼續(xù)創(chuàng)建 book對象,但這有點麻煩——不過對象構(gòu)造函數(shù)有助于再次使用對象字面量。

 
 
 
 
  1. functionBook(title, author, year) { 
  2.  
  3. this.title = title; 
  4.  
  5. this.author = author; 
  6.  
  7. this.year = year; 
  8.  
  9. }const book1 = new Book ('Hippie', 'Paulo Coelho', 
  10.  
  11. '2018'); 
  12.  
  13. console.log(book1); 
  14.  
  15. > Book { 
  16.  
  17. title: "Hippie", 
  18.  
  19. author: "Paulo Coelho", 
  20.  
  21. year: "2018" 
  22.  
  23. }// if we want to create more than onebook just we call 
  24.  
  25. function book with new keyword.const book2 
  26.  
  27. = new Book ('TheAlchemist', 'Paulo Coelho', '1988'); 

book1 和 book2創(chuàng)建 Book的實例并將其分配給變量。想知道一個對象是否是另一個對象的實例??梢杂胕nstanceof。

 
 
 
 
  1. book1 instanceof Book 
  2.  
  3. > true 

1.3 Object.create()方法

JavaScript中的每個對象都將從主對象創(chuàng)建。任何時候使用大寫字母“O”時,指的都是主對象。我們可以在console控制臺中打印主對象。主對象有很多方法,下面來看object.create()方法。

Object.create()創(chuàng)建法使用現(xiàn)有對象作為原型來創(chuàng)建新對象?;菊Z法如下:

 
 
 
 
  1. Object.create(proto,[propertiesObject]) 

proto是新建對象的原型。 propertiesObject是一個可選項。

下面舉個簡單的例子:

 
 
 
 
  1. constBook = { 
  2.  
  3. summary : function() { 
  4.  
  5. console.log(`${this.title} iswritten by ${this.author}.`) 
  6.  
  7.  
  8. }const book1 = Object.create(Book); 
  9.  
  10. book1.author = "Paulo Coelho"; 
  11.  
  12. book1.title = "Hippie";console.log(book1.summary()); 
  13.  
  14. > Hippie is written by Paulo Coelho. 

以上的例子創(chuàng)建了一個原始對象book1,并為作者和標題賦值。可以看到原始對象中的匯總函數(shù):

下面將Object.create() 方法進行詳細介紹。

2. 類

類不是對象,它是對象的藍本,是特殊函數(shù)??梢允褂煤瘮?shù)的表達式和聲明來定義函數(shù),也可以這樣定義類。藍本可用來表示對象的數(shù)量。

可以使用類的關(guān)鍵字和名稱。語法與Java相似。

類語法是使用面向?qū)ο缶幊毯凸芾碓偷囊粋€好途徑:

 
 
 
 
  1. let Book= function(name) { 
  2.  
  3. this.name = name 
  4.  
  5. }let newBook = function(name) { 
  6.  
  7. Book.call(this, name) 
  8.  
  9. } newBook.prototype = Object.create(Book.prototype); 
  10.  
  11. const book1 = new newBook("The Alchemist"); 

此例使用了ES6類語法:

 
 
 
 
  1. classBook { 
  2.  
  3. constructor(name) { 
  4.  
  5. this.name = name 
  6.  
  7.  
  8. }class newBook extends Book { 
  9.  
  10. constructor(name) { 
  11.  
  12. super(name); 
  13.  
  14.  
  15. }const book1 = new newBook("The Alchemist"); 

類語法是語法糖(syntactical sugar)—而場景背后它仍然使用基于原型的模型。類是函數(shù),而函數(shù)是JavaScript中的對象。

 
 
 
 
  1. classBook { 
  2.  
  3. constructor(title, author){ 
  4.  
  5. this.title = title; 
  6.  
  7. this.author = author; 
  8.  
  9.  
  10. summary() { 
  11.  
  12. console.log(`${this.title} writtenby ${this.author}`); 
  13.  
  14.  
  15. }const book1 = new Book("", ""); 
  16.  
  17. console.log(typeof Book); 
  18.  
  19. > "function"console.log(typeof book1); 
  20.  
  21. > "object" 

3. 封裝(Encapsulation)

封裝意為隱藏信息或數(shù)據(jù)。指對象在不向外部使用者透露任何執(zhí)行細節(jié)的情況下執(zhí)行其功能。換句話說,就是其私有變量只對當前函數(shù)可見,而對全局范圍或其他函數(shù)不可訪問。

 
 
 
 
  1. constBook = function(t, a) { 
  2.  
  3. let title = t; 
  4.  
  5. let author = a; 
  6.  
  7. return { 
  8.  
  9. summary : function() { 
  10.  
  11. console.log(`${title} written by${author}.`); 
  12.  
  13.  
  14.  
  15.  
  16. const book1 = new Book('Hippie', 'Paulo Coelho'); 
  17.  
  18. book1.summary(); 
  19.  
  20. > Hippie written by Paulo Coelho. 

在上面的代碼中,標題和作者只在函數(shù)Book 的范圍內(nèi)可見,方法summary對Book的使用者可見。所以書名和作者被封裝在Book中。

4. 抽象

抽象意為實現(xiàn)隱藏。它是一種隱藏實現(xiàn)細節(jié)的方法,只向使用者顯示基本特性。換句話說,它隱藏了不相關(guān)的細節(jié),只顯示了必須對外部世界顯示的。缺乏抽象會導(dǎo)致代碼出現(xiàn)可維護性問題。

 
 
 
 
  1. constBook = function(getTitle, getAuthor) { 
  2.  
  3. // Private variables / properties 
  4.  
  5. let title = getTitle; 
  6.  
  7. let author = getAuthor;// Publicmethod 
  8.  
  9. this.giveTitle = function() { 
  10.  
  11. return title; 
  12.  
  13.  
  14. // Private method 
  15.  
  16. const summary = function() { 
  17.  
  18. return `${title} written by${author}.` 
  19.  
  20. }// Public method that has access toprivate method. 
  21.  
  22. this.giveSummary = function() { 
  23.  
  24. return summary() 
  25.  
  26.  
  27. }const book1 = new Book('Hippie', 'Paulo Coelho'); 
  28.  
  29. book1.giveTitle(); 
  30.  
  31. > "Hippie"book1.summary(); 
  32.  
  33. > Uncaught TypeError: book1.summary is not a 
  34.  
  35. functionbook1.giveSummary(); 
  36.  
  37. > "Hippie written by Paulo Coelho." 

5. 復(fù)用/繼承

JavaScript繼承是一種機制,允許我們使用現(xiàn)有的類創(chuàng)建一個新類。也就是子類繼承父類的所有屬性和行為。

一般來說,JavaScript不是一種基于類的語言。關(guān)鍵字“類”是在ES6中引入的,但它是語法糖,JavaScript仍然是基于原型的。在JavaScript中,繼承是通過使用原型來實現(xiàn)的。這種模式稱為行為委托模式或原型繼承。

同樣可以通過book例子來體現(xiàn):

 
 
 
 
  1. functionBook(title, author, year) { 
  2.  
  3. this.title = title; 
  4.  
  5. this.author = author; 
  6.  
  7. this.year = year; 
  8.  
  9. this.summary = function() { 
  10.  
  11. console.log(`${this.title} iswritten by ${this.author}.`) 
  12.  
  13.  
  14.  
  15. const book1 = new Book ('Hippie', 'Paulo Coelho', '2018'); 
  16.  
  17. const book2 = newBook ('The Alchemist', 'Paulo Coelho', 
  18.  
  19. '1988'); 

原型繼承

對于Book的每個實例,我們都在為基類中的方法重建內(nèi)存。這些方法必須在所有實例之間共享 — 不應(yīng)特定于個別實例中。圖中的原型是:

 
 
 
 
  1. letCorebook = function(title) { 
  2.  
  3. this.title = title 
  4.  
  5. }Corebook.prototype.title = function() { 
  6.  
  7. console.log(`name of the book is${this.title}`); 
  8.  
  9. }Corebook.prototype.summary = function(author) { 
  10.  
  11. console.log(`${this.title} is writtenby ${this.author}`); 
  12.  
  13. }let Book = function(title, author) { 
  14.  
  15. Corebook.call(this, title, author) 
  16.  
  17. }Book.prototype = Object.create(Corebook.prototype); 
  18.  
  19. let book1 
  20.  
  21. = new Book('TheAlchemist', 'Paulo Coelho');book1.title(); 
  22.  
  23. > name of the book is The Alchemistbook1.summary(); 
  24.  
  25. > The Alchemist is written by Paulo Coelho 

在上面的代碼中,Book 的實例有一個原型的副本,能夠鏈接到Book的原型,而Book的原型又鏈接到Corebook的原型。

6. 多態(tài)

在不同的對象上使用同一方法,并讓每個對象具有自己的表現(xiàn)形式或形態(tài)的能力,稱為多態(tài)。

 
 
 
 
  1. letbook1 = function () {} 
  2.  
  3. book1.prototype.summary = function() { 
  4.  
  5. return "summary of book1" 
  6.  
  7. }let book2 = function() {} 
  8.  
  9. book2.prototype = Object.create(book1.prototype); 
  10.  
  11. book2.prototype.summary = function() { 
  12.  
  13. return "summary of book2" 
  14.  
  15. }let book3 = function() {} 
  16.  
  17. book3.prototype = Object.create(book1.prototype); 
  18.  
  19. book3.prototype.summary = function() { 
  20.  
  21. return "summary of book3" 
  22.  
  23.  
  24. var books = [new book1(), new book2(), new book3()]; 
  25.  
  26. books.forEach(function(book){ 
  27.  
  28. console.log(book.summary()); 
  29.  
  30. });> summary of book1 
  31.  
  32. > summary of book2 
  33.  
  34. > summary of book3 

對象之間的關(guān)系將由關(guān)聯(lián)、聚合和組合定義。

7. 關(guān)聯(lián)

關(guān)聯(lián)是兩個或多個對象之間的關(guān)系。每個對象都是獨立的。換句話說,關(guān)聯(lián)定義了對象之間的多重性:一對一、一對多、多對一、多對多。

 
 
 
 
  1. functionBook(title, author) { 
  2.  
  3. this.title = title; 
  4.  
  5. this.author = author; 
  6.  
  7.  
  8. const book1 = new Book ('Hippie', 'Paulo Coelho'); 
  9.  
  10. const book2 = new Book ('TheAlchemist', 
  11.  
  12. 'Paulo Coelho'); 
  13.  
  14. book2.multiplicity 
  15.  
  16. = book1 

book1 賦值于book2的屬性多樣化,顯示對象book1 和 book2之間的關(guān)系。兩者都可以獨立添加和刪除。

8. 聚合

聚合是關(guān)聯(lián)的特例。在兩個對象之間的關(guān)系中,一個對象可能比另一個更重要。換句話說,當一個對象比另一個擁有更多的所有權(quán)時,這就是聚合。對象所有者通常稱為聚合,被所有者稱為組件。聚合又叫“Has-a”關(guān)系。

 
 
 
 
  1. functionBook(title, author) { 
  2.  
  3. this.title = title; 
  4.  
  5. this.author = author; 
  6.  
  7.  
  8. const book1 = new Book ('Hippie', 'Paulo Coelho'); 
  9.  
  10. const book2 = new Book ('TheAlchemist', 'Paulo Coelho'); 
  11.  
  12. let publication = { 
  13.  
  14. "name": "new publicationInc", 
  15.  
  16. "books": [] 
  17.  
  18. }publication.books.push(book1); 
  19.  
  20. publication.books.push(book2); 

book1 和 book2 被添加到對象publication下設(shè)的books中。如果在book1和book2 運行之后刪除publication,則 Book和 publication 都將獨立運行。

9. 組合

組合是聚合的一種特殊情況。一個對象包含另一個對象,并且被包含的對象脫離后無法生存。

 
 
 
 
  1. let Book= { 
  2.  
  3. "title": "TheAlchemist", 
  4.  
  5. "author": "PauloCoelho", 
  6.  
  7. "publication": { 
  8.  
  9. "name": "newpublication Inc", 
  10.  
  11. "address":"chennai" 
  12.  
  13.  

這里屬性publication與 Book 對象有嚴格的限制,publication不能沒有Book對象。如果Book的id被刪除,則publication也將被刪除。

重組合輕繼承

繼承指一個對象基于另一個對象的情況。例如,book1繼承了標題、作者和結(jié)語等書籍的屬性和方法,所以它建立了book1 is-a Book關(guān)系。

組合是收集單一對象并將它們組合起來構(gòu)建更復(fù)雜的對象。為構(gòu)建book1,需要一些方法,比如紙和筆。因此book1 has-a paper and a pen關(guān)系隨之出現(xiàn)。

 
 
 
 
  1. constgetTitle = (data) => ({ 
  2.  
  3. title : () => console.log(`title :${data.title}`) 
  4.  
  5. });const getAuthor = (data) => ({ 
  6.  
  7. author : () => console.log(`author:${data.author}`) 
  8.  
  9. });const getSummary = () => ({ 
  10.  
  11. summary :() => console.log(`booksummary need to 
  12.  
  13. update.`) 
  14.  
  15. });const Book = (title, author) => { 
  16.  
  17. const data = { 
  18.  
  19. title, 
  20.  
  21. author 
  22.  
  23.  
  24. return Object.assign({}, 
  25.  
  26. getTitle(data), 
  27.  
  28. getAuthor(data), 
  29.  
  30. getSummary() 
  31.  
  32.  
  33. }let book1 = Book('The Alchemist', 'Paulo Coelho'); 
  34.  
  35. book1.title(); 
  36.  
  37. > "title : The Alchemist" 

文章題目:代碼詳解:使用JavaScript進行面向?qū)ο缶幊痰闹改?
網(wǎng)站路徑:http://www.dlmjj.cn/article/djhoesc.html