javascript 其他有用的JavaScript工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 其他有用的JavaScript工具相关的知识,希望对你有一定的参考价值。

export const assign = (...objs) => Object.assign({}, ...objs)
export const assign2 = (...objs) => {
  objs.unshift({});
  let len = objs.length;
  while (--len > 0)
    for (let i in objs[len])
      objs[0][i] = objs[len][i]
  return objs[0]
}
// no spread operator overhead, raw arguments access, obj[0] *is* mutated!
export const assign3 = () => {
  for (let i = 1; i < arguments.length; i++)
    for (let j in arguments[i])
      arguments[0][j] = arguments[i][j]
  return arguments[0]
}

export const defaults = (...objs) => assign(...objs.reverse())
export const defaults2 = (...objs) => assign2(...objs.reverse())
export const defaults3 = (...objs) => assign3(...([{}].concat(objs.reverse())))

export const cloneDeep = obj => JSON.parse(JSON.stringify(obj))

export const delay = (ms, value) => new Promise(res => setTimeout(res, ms, value))

export const sortNums = (a, b) => (a - b)

export const avg = (...args) => (args.reduce((total, i) => { total += i; return total; }, 0) / args.length)

// see: https://github.com/developit/dlv
export const getProp = (obj = {}, keys = '', defVal) => {
  keys = keys.split('.')
  while (obj && keys.length) obj = obj[keys.shift()]
  return (obj == null ? defVal : obj) // note: handle null and undefined check using `==`
};

以上是关于javascript 其他有用的JavaScript工具的主要内容,如果未能解决你的问题,请参考以下文章

11中javascrip教程教不到的小技巧

JavaScrip笔记心得(持续更新)

JavaScrip笔记心得(持续更新)

JavaScrip笔记心得(持续更新)

JavaScrip和Java一样吗?

JavaScrip入门