Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

这句JS代码是什么意思?

image.png

const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

就是取嵌套属性值:

let data = {
  a: {
    b: {
      c: {
        d: 1
      }
    }
  }
}

pluckDeep('a.b.c.d')(data) //返回1

说实话写得极其糟糕,这样的代码短但并不简洁,并且没处理空值。

好的代码,应该易读、易用,并且健壮。

如果实现这样一个接口,暴露给外部应该是直接接收2个参数:obj和keyString。而不是像现在接收一个参数,返回一个函数,再接收一个参数,再返回结果,反直观思维。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...