前端工程师笔试题目-js篇

本文详细介绍了JavaScript中的预编译过程,包括AO对象的创建、实参与形参的统一以及函数声明的处理。同时,探讨了this在不同调用场景下的指向问题,以及在函数和对象方法中的应用。最后,讨论了箭头函数的特性,特别是其this值的绑定规则。通过实例解析,帮助读者深入理解JavaScript的基础概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、js预编译

步骤:

  • 1、创建AO对象(AO:{})
  • 2、找到形参和对象的声明(abc),作为对象的属性名,值为undifined
  • 3、实参和形参相统一(a=1,b=2)
  • 4、找到函数声明,会覆盖变量的声明(a=function a() { },b=function b() { })

习题:

<script>
	function test(a, b) {
		console.log(a)  //  function a() { }
		var a = 123
		console.log(a)  //  123
		console.log(b)  //  function b() { }
		function a() { }
		if (false) { var c = 123 } //  为假,不执行
		console.log(c)  //  undefined
		var c = function () { }
		console.log(c)  //  function () { }
		var b = 123
		console.log(b)  //  123
		function b() { }
	}
	test(1, 2)

	// - 预编译
	// - 1、创建AO对象
	// - 2、找到形参和对象的声明,作为对象的属性名,值为undifined
	AO: {
		a: undefined
		b: undefined
		c: undefined
	}
	// - 3、实参和形参相统一
	AO: {
		a: undefined, 1
		b: undefined, 2
		c: undefined
	}
	// - 4、找到函数声明,会覆盖变量的声明,(var c = function () { }是定义,不是声明)
	AO: {
		a: undefined, 1, function a() { }
		b: undefined, 2, function b() { }
		c: undefined
	}
</script>


2、js-this

  • 1、函数被直接调用:fun.call(window)
  • 2、函数作为对象被调用:fun.run.call(fun)

习题:

<script>
	//  1、在函数中直接使用
	function get(content) {
		console.log(content)
	}
	get('hello,world')  //  1、相当于  get.call(window, 'hello,world')

	//  2、函数作为对象被调用
	var person = {
		name: 'jasmine',
		run: function (time) {
			console.log(`${this.name},${time}`)
		}
	}
	person.run(30)  //  2、相当于  person.run.call(person,30)



	var name = 222
	var a = {
		name: 111,
		say: function () {
			console.log(this.name)
		}
	}

	var fun = a.say
	fun()    //  1、相当于  fun.call(window),结果为:222
	a.say()  //  2、相当于  a.say.call(a),结果为:111

	var b = {
		name: 333,
		say: function (fun) {
			fun()
		}
	}
	b.say(a.say)  //  1、fun()  相当于  fun.call(window),结果为:222
	b.say = a.say
	b.say()  //  2、相当于  b.say.call(b),结果为:333
</script>

3、js箭头函数的this

  • 简言之,一句话:箭头函数的this就是外层代码块的this,箭头函数不能当作构造函数
<script>
	//  简言之,一句话:箭头函数的this就是外层代码块的this,箭头函数不能当作构造函数

	var a = 111
	var test = {
		a: 222,
		say: () => {
			console.log(this.a)
		}
	}
	test.say()  //  say()函数跟test对象平级,所以调用a=111,结果为:111


	var test = {
		a: 333,
		say: function () {
			console.log(this.a)
		}
	}
	test.say()  //  say()函数写在function里面,say()函数比test对象低一级,所以调用a=333,结果为:333
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jasmine_qiqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值