分享 underscore 的两个坑
 发布于 11 年前  作者 jysperm  4479 次预览  最后一次回复是 11 年前  来自 分享 

1 . change log 1.7.0 _.template 有不兼容的修改。

Underscore templates no longer accept an initial data object. _.template always returns a function now.

2 . _.extend(destination, *sources) _.extend 不返回一个新的对象, 而是直接修改 destination.

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

这两个坑曾各浪费我一个小时的时间。

6 回复
chloe

文档不是说得很明白吗?

chloe

还有这个copy也不是深度clone遇到引用时,会直接返回引用

var a = {a:{x:1}}; 如var b = _.extend({},a); b.a.x = 2; 现在a.a.x 也等于2

yaochun

api文档要看仔细啊

其实_.extend 这个严格意义上应该有第3个参数,来决定是否覆盖destination还是新建一个{}来做

jysperm

主要是 underscore 大多数函数都是返回一个新的对象,比如 filter, reject, last, without 什么的。

koroshi

学习到了- -~

tamamaxox

要深度clone 请用lodash