国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS克隆,屬性,數組,對象,函數實例分析

2019-11-19 18:48:45
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朔州市| 五莲县| 成都市| 镇原县| 昭苏县| 施甸县| 彭山县| 秭归县| 五莲县| 忻城县| 大田县| 龙胜| 福鼎市| 河北区| 仁化县| 三穗县| 宣汉县| 梅河口市| 金川县| 宜川县| 微博| 雷山县| 堆龙德庆县| 桦甸市| 和林格尔县| 宁远县| 邹城市| 鲁山县| 宁蒗| 红桥区| 兴业县| 塘沽区| 响水县| 洛隆县| 来凤县| 旬阳县| 乐安县| 珲春市| 山东省| 阜新| 理塘县|