CNode

分享 underscore 的两个坑

分享
Jjysperm发布于12 年前最后回复12 年前6 回复4627 浏览0 收藏

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)

C
chloe#1·12 年前

文档不是说得很明白吗?

C
chloe#2·12 年前

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

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

Y
yaochun#3·12 年前

api文档要看仔细啊

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

J
jysperm#4·12 年前

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

K
koroshi#5·12 年前

学习到了- -~

T
tamamaxox#6·12 年前

要深度clone 请用lodash

参与回复
登录后即可参与回复。登录