JS克隆,屬性,數(shù)組,對(duì)象,函數(shù)實(shí)例分析
本文實(shí)例講述了JS克隆,屬性,數(shù)組,對(duì)象,函數(shù)。分享給大家供大家參考,具體如下:
<script type="text/javascript"> /* 克隆原型得到對(duì)象 */ function clone(object) { function F() {} F.prototype = object; return new F; } var Person = { name: 'default name', getName: function() { return this.name; } }; var reader = clone(Person); console.log(reader.getName()); // This will output 'default name'. reader.name = 'John Smith'; console.log(reader.getName()); // This will now output 'John Smith'. /* Author Prototype Object. */ var Author = clone(Person); Author.books = []; // 書(shū)數(shù)組 Author.getBooks = function() { return this.books; } var author = []; author[0] = clone(Author); author[0].name = 'Dustin Diaz'; author[0].books = ['JavaScript Design Patterns']; author[1] = clone(Author); author[1].name = 'Ross Harmes'; author[1].books = ['JavaScript Design Patterns','PHP','Mysql']; console.log(author[0].getName()); console.log(author[0].getBooks()); console.log(author[1].getName()); console.log(author[1].getBooks()); </script>
結(jié)果
這里的console.log很有意思,比alert有意思,alert不能獲取全部數(shù)據(jù),需要一個(gè)個(gè)彈出。
js的數(shù)組定義也很有意思。
進(jìn)一步升級(jí)
<script type="text/javascript"> /* 克隆原型得到對(duì)象 */ function clone(object) { function F() {} F.prototype = object; return new F; } var Person = { name: 'default name', getName: function() { return this.name; } }; var Author = clone(Person); Author.books = []; // 書(shū)數(shù)組 Author.getBooks = function() { return this.books; } var authorClone = clone(Author); console.log(authorClone.name); // string 'default name'. authorClone.name = 'new name'; // 重新賦值 console.log(authorClone.name); // Now linked to the primative authorClone.name, which // is the string 'new name'. console.log(Author.getName()); // 沒(méi)有改變,任然是 'default name' console.log(Author.getBooks()); // 空的 authorClone.books.push('new book'); // Author被改了 authorClone.books.push('new new book'); // Author被改了 console.log(Author.getBooks()); // array 'new book' console.log(authorClone.getBooks()); // array 'new book' authorClone.books = []; // 定義了屬于自己的books數(shù)組 authorClone.books.push('new book2'); // We are now modifying that new array. authorClone.books.push('new book3'); authorClone.books.push('new book4'); console.log(authorClone.getBooks()); console.log(Author.getBooks()); var CompoundObject = { string1: 'default value', childObject: { bool: true, num: 10 }, getChild: function() { // 返回對(duì)象Object return this.childObject; } } var compoundObjectClone = clone(CompoundObject); compoundObjectClone.childObject.num = 5; // 不好的方式 compoundObjectClone.childObject = { // 好一點(diǎn)的方式 bool: true, num: 5 }; console.log(compoundObjectClone.getChild()); </script>
結(jié)果:
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript常用函數(shù)技巧匯總》、《javascript面向?qū)ο笕腴T(mén)教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JS/HTML5游戲常用算法之路徑搜索算法 A*尋路算法完整實(shí)例
這篇文章主要介紹了JS/HTML5游戲常用算法之路徑搜索算法 A*尋路算法,結(jié)合完整實(shí)例形式分析了A*尋路算法的具體實(shí)現(xiàn)技巧,代碼備有詳盡的注釋便于理解,需要的朋友可以參考下2018-12-12BootStrap fileinput.js文件上傳組件實(shí)例代碼
這篇文章主要介紹了BootStrap fileinput.js文件上傳組件實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02javascript實(shí)現(xiàn)計(jì)時(shí)器的簡(jiǎn)單方法
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)計(jì)時(shí)器的簡(jiǎn)單方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02layui 實(shí)現(xiàn)加載動(dòng)畫(huà)以及非真實(shí)加載進(jìn)度的方法
今天小編就為大家分享一篇layui 實(shí)現(xiàn)加載動(dòng)畫(huà)以及非真實(shí)加載進(jìn)度的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享,本文給出了2種防嵌入方法,需要的朋友可以參考下2014-12-12