《设计模式 》 - 4. 原型模式( Prototype )
来源:互联网 发布:ubuntu挂载nfs 编辑:程序博客网 时间:2024/05/13 12:55
原型模式 ( Prototype ) :
语言 : JavaScript
JavaScript 克隆方法 :
function clone(obj) { if (null == obj || "object" != typeof obj) { return obj; } // Date if (obj instanceof Date) { var copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Array if (obj instanceof Array) { var copy = []; var len = obj.length; for (var i = 0; i < len; ++i) { copy[i] = clone(obj[i]); } return copy; } // Object if (obj instanceof Object) { var copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); } return copy; } throw new Error("Unable to copy");}
客户类 :
var person = { name : "小明", age : "15", eat : function(){ console.log("eating"); }}var clonePersonA = clone(person);console.log(clonePersonA);clonePersonA.eat();
代码图示 :
结果显示 :
1 0
- 设计模式 原型Prototype
- prototype(原型设计模式)
- 设计模式--Prototype(原型)
- Prototype(原型)设计模式
- 设计模式-prototype原型
- 《设计模式 》 - 4. 原型模式( Prototype )
- 设计模式--原型模式(ProtoType)
- [设计模式]Prototype原型模式
- 设计模式--Prototype(原型模式)
- 设计模式 -- 原型模式Prototype
- 设计模式-原型模式(Prototype)
- [设计模式] 原型模式(Prototype)
- 设计模式-原型模式(Prototype)
- 【设计模式】-原型模式-Prototype
- 设计模式 原型模式 Prototype
- 设计模式:原型模式-prototype
- 设计模式---原型模式(Prototype)
- 设计模式 原型设计 Prototype
- 阿里云 docker 容器源
- loadrunner脚本模板
- 动态的改变Textview的margin属性
- Maven实战指南 08
- MediaMetadataRetriever小试牛刀
- 《设计模式 》 - 4. 原型模式( Prototype )
- 第三节 file_get_contents()和fopen()模拟表单提交
- 根据本地/服务器时间获取指定时区时间 new Date指定时区时间
- 接口应用:XML序列化及二进制图片转换字符串
- 【数据结构与算法】 PROJECT 3 有向图的最短路径实现 之 知识点
- eclipse中Outline与Variables下各图标的意思
- AndroidManifest清单文件合并失败 Error:Execution...Manifest merger failed with multiple errors, see logs
- 身份证阅读器代码C#
- 访问本地