From eebed2e6ed9dd9ffda0b557799bbf22d4a45cb7f Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Mon, 15 Apr 2019 13:17:04 +0800 Subject: [PATCH] core.clone filter --- _docs/api.md | 10 +++++++++- libs/utils.js | 11 +++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/_docs/api.md b/_docs/api.md index 2f167244..4194aa66 100644 --- a/_docs/api.md +++ b/_docs/api.md @@ -1913,9 +1913,17 @@ errorCallback可选,如果失败,则会将错误信息传入errorCallback() 此函数是异步的,只能通过回调函数来获得读取的结果或错误信息。 -core.clone(data) +core.clone(data, filter, recursion) 深拷贝一个对象。有关浅拷贝,深拷贝,基本类型和引用类型等相关知识可参见: https://zhuanlan.zhihu.com/p/26282765 +filter为过滤函数,如果设置且不为null则需传递一个可接受(name, value)的函数, +并返回true或false,表示该项是否应该被深拷贝。 +recursion表示该filter是否应递归向下传递,如果为true则递归函数也将传该filter。 +例如: +core.clone(core.status.hero, function(name, value) { + return name == 'items' || typeof value == 'number'; +}, false); +这个例子将会深拷贝勇士的属性和道具。 core.splitImage(image, width, height) diff --git a/libs/utils.js b/libs/utils.js index a527f9e9..9260a9c7 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -247,7 +247,7 @@ utils.prototype.removeLocalForage = function (key, successCallback, errorCallbac } ////// 深拷贝一个对象 ////// -utils.prototype.clone = function (data) { +utils.prototype.clone = function (data, filter, recursion) { if (!core.isset(data)) return null; // date if (data instanceof Date) { @@ -258,10 +258,9 @@ utils.prototype.clone = function (data) { // array if (data instanceof Array) { var copy = []; - // for (var i=0;i