JS匿名函數(shù)類生成方式實(shí)例分析
本文實(shí)例講述了JS匿名函數(shù)類生成方式。分享給大家供大家參考,具體如下:
<script type="text/javascript"> var Book = (function() { // 私有靜態(tài)屬性 var numOfBooks = 0; // 私有靜態(tài)方法 function checkIsbn(isbn) { if(isbn == undefined || typeof isbn != 'string') { return false; } return true; } // 返回構(gòu)造函數(shù) return function(newIsbn, newTitle, newAuthor) { // implements Publication // 私有屬性 var isbn, title, author; // 特權(quán)方法 this.getIsbn = function() { return isbn; }; this.setIsbn = function(newIsbn) { if(!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.'); isbn = newIsbn; }; this.getTitle = function() { return title; }; this.setTitle = function(newTitle) { title = newTitle || 'No title specified'; }; this.getAuthor = function() { return author; }; this.setAuthor = function(newAuthor) { author = newAuthor || 'No author specified'; }; // 控制對(duì)象數(shù)目,構(gòu)造函數(shù) numOfBooks++; // Keep track of how many Books have been instantiated // with the private static attribute. if(numOfBooks > 5) throw new Error('Book: Only 5 instances of Book can be ' + 'created.'); this.setIsbn(newIsbn); this.setTitle(newTitle); this.setAuthor(newAuthor); } })(); // 公有靜態(tài)方法 Book.convertToTitleCase = function(inputString) { alert('convertToTitleCase'); }; // 公有非特權(quán)方法 Book.prototype = { display: function() { alert("isbn:"+this.getIsbn()+" title:"+this.getTitle()+" author:"+this.getAuthor()); } }; //var theHobbit = new Book(123, '', 'J. R. R. Tolkein'); // 非字符串拋出異常 var theHobbit = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit.display(); //theHobbit.convertToTitleCase(); // Uncaught TypeError: Object #<Object> has no method 'convertToTitleCase' Book.convertToTitleCase(); // 輸出convertToTitleCase var theHobbit2 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit2.display(); var theHobbit3 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit3.display(); var theHobbit4 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit4.display(); var theHobbit5 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit5.display(); var theHobbit6 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit6.display(); // Uncaught Error: Book: Only 5 instances of Book can be created. </script>
這里已經(jīng)把js出神入化了,佩服到極致,代碼清晰簡潔,美觀,注釋恰到好處。
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript常用函數(shù)技巧匯總》、《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- js 數(shù)組克隆方法 小結(jié)
- javascript克隆對(duì)象深度介紹
- js獲取和設(shè)置屬性的方法
- 顯示js對(duì)象所有屬性和方法的函數(shù)
- js數(shù)組循環(huán)遍歷數(shù)組內(nèi)所有元素的方法
- js數(shù)組與字符串的相互轉(zhuǎn)換方法
- JS 創(chuàng)建對(duì)象(常見的幾種方法)
- JS匿名函數(shù)實(shí)例分析
- JS 面向?qū)ο笾衿娴膒rototype
- JS面向?qū)ο缶幊讨畬?duì)象使用分析
- JS面向?qū)ο缶幊虦\析
- JS克隆,屬性,數(shù)組,對(duì)象,函數(shù)實(shí)例分析
相關(guān)文章
js中異步函數(shù)async function變同步函數(shù)的簡單入門
這篇文章主要介紹了js中異步函數(shù)async function變同步函數(shù)的簡單入門,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04js實(shí)現(xiàn)拖拽 閉包函數(shù)詳細(xì)介紹
在開發(fā)過程中可能會(huì)使用js實(shí)現(xiàn)拖拽等相關(guān)功能,本文將以此問題進(jìn)行深入介紹,需要了解的朋友可以參考下2012-11-11javascript對(duì)talbe進(jìn)行動(dòng)態(tài)添加、刪除、驗(yàn)證實(shí)現(xiàn)代碼
javascript對(duì)talbe進(jìn)行動(dòng)態(tài)添加、刪除、驗(yàn)證實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-03-03你應(yīng)該了解的JavaScript Array.map()五種用途小結(jié)
大家都知道m(xù)ap() 方法返回一個(gè)新數(shù)組,數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值。下面這篇文章主要給大家介紹了關(guān)于JavaScript Array.map()的五種用途,需要的朋友可以參考下2018-11-11uniapp實(shí)現(xiàn)滾動(dòng)觸底加載項(xiàng)目實(shí)戰(zhàn)
這篇文章主要為大家介紹了uniapp實(shí)現(xiàn)滾動(dòng)觸底加載項(xiàng)目實(shí)戰(zhàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09