js基础问题

1.函数作为参数传值,后面加个括号则为立即执行该函数后,返回结果作为参数传值
例如:

function func1(cb) {
  console.log('func1')
}
function func2(arg1, arg2) {
  console.log('func2')
  return function() {
    console.log('func3')
  }
}
func1(func2('arg1', 'arg2'))
//打印结果:func2 func1

2.箭头函数没有自己的执行上下文,也没有arguments,做参数传递也不能通过call绑定this,细品

let that = this
function a(func) {
  let aa = 2
  console.log(that === this)
  func.call(this, aa, this)
}

a(function(aa, that) {
  console.log(aa, that === this)
})
//  打印结果:
//  false
//  2 true
let that = this
function a(func) {
  let aa = 2
  console.log(that === this)
  func.call(this, aa, this)
}

a((aa, that) => {
  console.log(aa, that === this)
})
//  打印结果:
//  false
//  2 false
let that = this
function a(func) {
  let aa = 2
  console.log(that === this)
  func.call(this, aa, this)
}

a((aa, _that) => {
  console.log(aa, that === this)
})
//  打印结果:
//  false
//  2 true
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 9,945评论 0 5
  • 函数和对象 1、函数 1.1 函数概述 函数对于任何一门语言来说都是核心的概念。通过函数可以封装任意多条语句,而且...
    道无虚阅读 10,102评论 0 5
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,455评论 0 4
  • CSS和JS在网页中的放置顺序是怎么样的? 一般我们把CSS放在head头部标签中,把JS代码放在body代码的尾...
    羞涩的涩阅读 4,284评论 0 0
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 13,097评论 0 3