Skip to content

cloneWith

js
_.cloneWith(value, [customizer])

此方法与 _.clone 类似,不同之处在于它接受 customizer,调用 customizer 可生成克隆的值。如果 customizer 返回 undefined,则克隆由方法处理。customizer 使用最多四个参数来调用;(value [, index|key, object, stack])。

¥This method is like _.clone except that it accepts customizer which is invoked to produce the cloned value. If customizer returns undefined, cloning is handled by the method instead. The customizer is invoked with up to four arguments; (value [, index|key, object, stack]).

新增于

¥Since

4.0.0

参数

¥Arguments

  1. value (*):要克隆的值。

    ¥value ()*: The value to clone.

  2. [customizer] (函数):自定义克隆的函数。

    ¥[customizer] (Function): The function to customize cloning.

返回

¥Returns

(*):返回克隆的值。

¥()*: Returns the cloned value.

示例

¥Example

js
function customizer(value) {
  if (_.isElement(value)) {
    return value.cloneNode(false);
  }
}

var el = _.cloneWith(document.body, customizer);

console.log(el === document.body);
// => false
console.log(el.nodeName);
// => 'BODY'
console.log(el.childNodes.length);
// => 0

Lodash v4.17 中文网 - 粤ICP备13048890号