美文网首页
4.参数默认值&&不确定参数&&箭头函数

4.参数默认值&&不确定参数&&箭头函数

作者: yaoyao妖妖 | 来源:发表于2020-10-13 17:35 被阅读0次

参数默认值

function f (x, y, z) {
  if (y === undefined) {
    y = 7
  }
  if (z === undefined) {
    z = 42
  }
  return x + y + z
}

console.log(f(1, 8, 43))

function f (x, y = 7, z = x + y) {
  console.log(f.length)  // 代表函数参数 arguments,ES6中禁止使用 arguments,可以使用f.length 获取没有默认值的参数
  return x * 10 + z
}
console.log(f(1, undefined, 3, 4))

// 不确定参数的问题 es5 arguments es6
ES5

function sum () {
  let num = 0
  Array.prototype.forEach.call(arguments, function (item) {
    num += item * 1
  })
  // Array.from(arguments).forEach(function (item) {
  //   num += item * 1
  // })
  return num
}

ES6

function sum (base, ...nums) {
  // Rest parameter
  let num = 0
  nums.forEach(function (item) {
    num += item * 1
  })
  return base * 2 + num
}
console.log(sum(1, 2, 3))

function sum (x = 1, y = 2, z = 3) {
  return x + y + z
}
let data = [4, 5, 9]
// console.log(sum(data[0], data[1], data[2]))
// console.log(sum.apply(this, data)) // ES5 自动将数组对应到参数上去
// spread
console.log(sum(...data)) // ES6 

ES6 中的箭头函数 ()=>{}

function hello () {}
let hello = function () {}
let sum = (x, y, z) => {
  return {
    x: x,
    y: y,
    z: z
  }
}
console.log(sum(1, 2, 4))

let test = {
  name: 'test',
  say: () => {
    console.log(this.name, this)
  }
}
test.say()

学习视频记录

相关文章

  • es6之函数拓展

    本文目录 1.参数默认值 2.rest参数 3.箭头函数 4.箭头函数的简写技巧 1.参数默认值 默认参数就是当用...

  • ES6(五)—— 函数

    函数 函数(函数方法更新【默认值、不确定参数、箭头函数】) Default Parameters —— 如何处理函...

  • ES6--函数扩展

    函数新增特性 函数默认值,rest参数,扩展运算符,箭头函数,this绑定,尾调用 函数参数的默认值 rest参数...

  • 4.参数默认值&&不确定参数&&箭头函数

    参数默认值 // 不确定参数的问题 es5 arguments es6ES5 ES6 ES6 中的箭头函数 (...

  • ES6之函数扩展

    关键词:函数扩展 参数的默认值 属性的默认值 rest:获取函数的多余参数 箭头函数 箭头函数使用注意:1.函数体...

  • es6-函数扩展

    函数新增特性 参数默认值 rest参数 扩展运算符 箭头函数 this绑定 尾调用 参数默认值 注意:默认值后面必...

  • ES6知识点整理——函数扩展

    函数新增特性:参数默认值、rest参数、扩展运算符、箭头函数、this绑定、尾调用 1.参数默认值默认值后面不能有...

  • ES6入门之函数的扩展

    函数的扩展分为以下3个部分: 1 为函数参数指定默认值2 函数的 rest 参数3 箭头函数 为函数参数指定默认值...

  • es6--函数新增

    函数的扩展 函数参数设置默认值...rest参数箭头函数Promise函数Generator 函数async函数 ...

  • ES6新特性之 函数参数的默认值写法 和 箭头函数。

    1、函数参数的默认值ES5中不能直接为函数的参数指定默认值,只能通过以下的变通方式: 2、箭头函数箭头函数用 =>...

网友评论

      本文标题:4.参数默认值&&不确定参数&&箭头函数

      本文链接:https://www.haomeiwen.com/subject/izcqjktx.html