Skip to content

Instantly share code, notes, and snippets.

@viko16
Last active May 13, 2019 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viko16/a6e7a284ef082e15c14d1e6392bca04c to your computer and use it in GitHub Desktop.
Save viko16/a6e7a284ef082e15c14d1e6392bca04c to your computer and use it in GitHub Desktop.
兼容数组、对象解构的方法
// 比如 API 暴露
function useApi () {
const a = 'a'
const b = 'b'
const c = 'c'
return Object.assign([ a, b, c ], { a, b, c }) // 务必数组在前,否则无法维护下标
}
// 使用时既可以
const [ a, b, c ] = useApi() // react hook 风格的数组解构
// 也可以
const { a, b, c } = useApi() // 对象解构
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment