应对不存在的值

时间:2019-05-31 19:29:56

标签: javascript

我想知道我们是否复制一个不存在的值,复制的值的类型将是什么? 是null还是undefined

const args = [1]
console.log(...args.slice(1))

这是一个具体的例子

const compose = fns => (...args) =>
  fns.reduceRight((acc, fn) => fn(acc, ...args.slice(1)), args[0]);


const func3 = (x, y) => (y > 0 ? x + 3 : x - 3);

const func2 = x => x ** 2;

const func1 = x => x - 8;

const fnOne = compose([func1, func2, func3])('3', 1);
console.log(fnOne); // should be 1081

const fnTwo = compose([func1, func2, func3])('3', -1);
console.log(fnTwo); //should be -8

2 个答案:

答案 0 :(得分:1)

.slice始终返回一个数组,而不返回undefined。该数组可以为空。

如果调用函数的参数少于参数,则参数为undefined

  const fn = (a, b) => console.log(a, b);

 fn(1) // a = 1, b = undefined
 // equals
 fn(...[1])  // a = 1, b = undefined

答案 1 :(得分:1)

散布的空数组不返回任何参数。

console.log('>', ...[], '<');