本文实例讲述了JS克隆,属性,数组,对象,函数。分享给大家供大家参考,具体如下:
<script type="text/javascript"> /* 克隆原型得到对象 */ 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 = []; // 书数组 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>
结果
这里的console.log很有意思,比alert有意思,alert不能获取全部数据,需要一个个弹出。
js的数组定义也很有意思。
进一步升级
<script type="text/javascript"> /* 克隆原型得到对象 */ 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 = []; // 书数组 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()); // 没有改变,任然是 '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数组 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() { // 返回对象Object return this.childObject; } } var compoundObjectClone = clone(CompoundObject); compoundObjectClone.childObject.num = 5; // 不好的方式 compoundObjectClone.childObject = { // 好一点的方式 bool: true, num: 5 }; console.log(compoundObjectClone.getChild()); </script>
结果:
更多关于JavaScript相关内容可查看本站专题:《JavaScript常用函数技巧汇总》、《javascript面向对象入门教程》、《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】