We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
本文为读 lodash 源码的第三百零一篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash
gitbook也会同步仓库的更新,gitbook地址:pocket-lodash
import castPath from './.internal/castPath.js' import toKey from './.internal/toKey.js'
lodash源码分析之castPath
lodash源码分析之toKey
result 的作用跟 get 差不多,但是当传入的路径 path 对应的值是函数时, result 会执行这个函数,并且 this 会绑定到它的父级对象。
result
get
path
this
源码如下:
function result(object, path, defaultValue) { path = castPath(path, object) let index = -1 let length = path.length if (!length) { length = 1 object = undefined } while (++index < length) { let value = object == null ? undefined : object[toKey(path[index])] if (value === undefined) { index = length value = defaultValue } object = typeof value === 'function' ? value.call(object) : value } return object }
path = castPath(path, object) let index = -1 let length = path.length if (!length) { length = 1 object = undefined }
先调用 castPath 来将 path 统一转换成路径数组的形式。
castPath
如果路径数组的长度为 0 ,表示路径为空,这种情况下是要返回 defaultValue 的,但是看到这里仅仅是将 length 设置成 1 和将 object 设置成 undefined 。
0
defaultValue
length
1
object
undefined
这样做的原因是为了统一逻辑,返回 defaultValue 的会在下面处理。
while (++index < length) { let value = object == null ? undefined : object[toKey(path[index])] if (value === undefined) { index = length value = defaultValue } object = typeof value === 'function' ? value.call(object) : value }
遍历路径,在开始后续逻辑之前,先判断 object 是否为 null 或 undefined 值,如果是,则将 value 设置为 undefined ,否则从 objecgt 上将对应的属性值取出。
null
value
objecgt
如果取出的 value 为 undefined ,则肯定再没有下一层的属性,因此将 index 设置为 length ,这样就不会再执行下一次循环,但是又会执行本次循环余下的逻辑。同时将 value 设置为 defaultValue ,这也是 path 的长度为 0 时,将 object 设置为 undefined 的原因。
index
接下来判断 value 是否为函数,如果是函数,则调用这个函数,并且将 this 绑定到 object ,这个 object 也就是它的父级对象。得到的结果赋值给 object ,如果不是函数, value 也赋值给 object ,这样就实现了一层一层地从 object 上取值,直到最后一层。
署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)
最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见:
作者:对角另一面
The text was updated successfully, but these errors were encountered:
No branches or pull requests
本文为读 lodash 源码的第三百零一篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash
gitbook也会同步仓库的更新,gitbook地址:pocket-lodash
依赖
lodash源码分析之castPath
lodash源码分析之toKey
源码分析
result
的作用跟get
差不多,但是当传入的路径path
对应的值是函数时,result
会执行这个函数,并且this
会绑定到它的父级对象。源码如下:
处理path长度为0的情况
先调用
castPath
来将path
统一转换成路径数组的形式。如果路径数组的长度为
0
,表示路径为空,这种情况下是要返回defaultValue
的,但是看到这里仅仅是将length
设置成1
和将object
设置成undefined
。这样做的原因是为了统一逻辑,返回
defaultValue
的会在下面处理。遍历
遍历路径,在开始后续逻辑之前,先判断
object
是否为null
或undefined
值,如果是,则将value
设置为undefined
,否则从objecgt
上将对应的属性值取出。如果取出的
value
为undefined
,则肯定再没有下一层的属性,因此将index
设置为length
,这样就不会再执行下一次循环,但是又会执行本次循环余下的逻辑。同时将value
设置为defaultValue
,这也是path
的长度为0
时,将object
设置为undefined
的原因。接下来判断
value
是否为函数,如果是函数,则调用这个函数,并且将this
绑定到object
,这个object
也就是它的父级对象。得到的结果赋值给object
,如果不是函数,value
也赋值给object
,这样就实现了一层一层地从object
上取值,直到最后一层。License
署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)
最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见:
作者:对角另一面
The text was updated successfully, but these errors were encountered: