手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JS克隆,属性,数组,对象,函数实例分析
JS克隆,属性,数组,对象,函数实例分析
摘要:本文实例讲述了JS克隆,属性,数组,对象,函数。分享给大家供大家参考,具体如下:/*克隆原型得到对象*/functionclone(obje...

本文实例讲述了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>

结果

JS克隆,属性,数组,对象,函数实例分析1

这里的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>

结果:

JS克隆,属性,数组,对象,函数实例分析2

希望本文所述对大家JavaScript程序设计有所帮助。

【JS克隆,属性,数组,对象,函数实例分析】相关文章:

JQuery中DOM事件冒泡实例分析

Js的Array数组对象详解

JQuery中Text方法用法实例分析

JavaScript中继承用法实例分析

jQuery插件制作之全局函数用法实例

jQuery聚合函数实例

jquery中map函数遍历数组用法实例

jQuery插件制作之参数用法实例分析

JS创建对象几种不同方法详解

javaScript中push函数用法实例分析

精品推荐
分类导航