Recursive/deep extend/assign in Underscore.js?

后端 未结 6 1369
遥遥无期
遥遥无期 2020-11-29 10:47

Is there any way to get Underscore.js extend function:

Copy all of the properties in the source objects over to the destination object, and return t

6条回答
  •  野性不改
    2020-11-29 11:46

    Kurt Milam has published a mixin that adds a deepExtend method to underscore.js. It even deals with regular expressions (if you want). Excerpt from the documentation:

    Mix it in with underscore.js: _.mixin({deepExtend: deepExtend});

    Call it like this: var myObj = _.deepExtend(grandparent, child, grandchild, greatgrandchild)

    Notes: Keep it DRY.

    This function is especially useful if you're working with JSON config documents. It allows you to create a default config document with the most common settings, then override those settings for specific cases. It accepts any number of objects as arguments, giving you fine-grained control over your config document hierarchy.

提交回复
热议问题